How to import module in Python - python-3.x

import hashing power of 16
The hashing power of 16.py is the same with my current working directory. The pop-up message is
SyntaxError: invalid syntax
How can I solve this issue?
sys.path.append(working directory)
This above is not working either.

As far as i know, you should remove spaces from filename, because that way it will not work.
If you really want to do this...
Try something like
hashing_power_of_16 = __import__("hashing power of 16")
Then you can refer by hashing_power_of_16 to that module.
But using spaces anywhere in naming is bad idea.

You need to rename your class. hashing power of 16 is not a recomended name. You can rename it something like hashingPowerOfSixteen. After that you can import the class.

I think Python identifiers can't have spaces. So my guess would be it's not working because your file name is having blank spaces. Rename your file(to have a name without spaces) and try again.
Thanks

import "name of the module"
in case if the module name is lengthy, u can do it as:
import "name of the module" as "short acronym"
For example
I have a module named "Mysq" which is nothing but a python file with a function that returns square of the number.
Now if want to use it another program (make sure that you are working in the same directory)
import Mysq as ps print(ps.sq(3))

Related

Turning a Dataframe into a Series with .squeeze("columns")

I'm studying how to work with data right now and so I'm following along with a tutorial for working with Time Series data. Among the first things he does is read_csv on the file path and then use squeeze=True to read it as a Series. Unfortunately (and as you probably know), squeeze has been depricated from read_csv.
I've been reading documentation to figure out how to read a csv as a series, and everything I try fails. The documentation itself says to use pd.read_csv('filename').squeeze('columns') , but, when I check the type afterward, it is always still a Dataframe.
I've looked up various other methods online, but none of them seem to work. I'm doing this on a Jupyter Notebook using Python3 (which the tutorial uses as well).
If anyone has any insights into why I cannot change the type in this way, I would appreciate it. I'm not sure if I've misunderstood the tutorial altogether or if I'm not understanding the documentation.
I do literally type .squeeze("columns") when I write this out because when I write a column name or index, it fails completely. Am I doing that correctly? Is this the correct method or am I missing a better method?
Thanks for the help!
shampoo = pd.read_csv('shampoo_with_exog.csv',index_col= [0], parse_dates=True).squeeze("columns")
I would start with this...
#Change the the stuff between the '' to the entire file path of where your csv is located.
df = pd.read_csv(r'c:\user\documents\shampoo_with_exog.csv')
To start this will name your dataframe as df which is kind of the unspoken industry standard the same as pd for pandas.
Additionally, this will allow you to use a "raw" (the r) string which makes it easier to insert directories into your python code.
Once you are are able to successfully run this you can simply put df in a separate cell in jupyter. This will show you what your data looks like from your CSV. Once you have done all that you can start manipulating your data. While you can use the fancy stuff in pd.read_csv() I mostly just try to get the data and manipulate it from the code itself. Obviously there are reasons not to only do a pd.read_csv but as you progress you can start adding things here and there. I almost never use squeeze although I'm sure there will be those here to comment stating how "essential" it is for whatever the specific case might be.

Can I format variables like strings in python?

I want to use printing command bellow in many places of my script. But I need to keep replacing "Survived" with some other string.
print(df.Survived.value_counts())
Can I automate the process by formating variable the same way as string? So if I want to replace "Survived" with "different" can I use something like:
var = 'different'
text = 'df.{}.value_counts()'.format(var)
print(text)
unfortunately this prints out "df.different.value_counts()" as as a string, while I need to print the value of df.different.value_counts()
I'm pretty sure alot of IDEs, have this option that is called refactoring, and it allows you to change a similar line of code/string on every line of code to what you need it to be.
I'm aware of VSCode's way of refactoring, is by selecting a part of the code and right click to select the option called change all occurances. This will replace the exact code on every line if it exists.
But if you want to do what you proposed, then eval('df.{}.value_counts()'.format(var)) is an option, but this is very unsecured and dangerous, so a more safer approach would be importing the ast module and using it's literal_eval function which is safer. ast.literal_eval('df.{}.value_counts()'.format(var)).
if ast.literal_eval() doesn't work then try this final solution that works.
def cat():
return 1
text = locals()['df.{}.value_counts'.format(var)]()
Found the way: print(df[var].value_counts())

python error-can only join an iterable

I am making a program in pygame and I am trying to create with buttons where the user can enter their name. So basically when ever they click on the letter, then it puts that letter into a list. I recreated with about the same variables as in my game and got the same error. So if we can fix this, then I can fix my game. I am using functions so that is why you see the global thing as that is needed. Here is the code. I have looked at other forums btw but nothing that is the same as mine.
import glob
unknown=[]
def bob():
global unknown
a=('a').upper()
unknown=unknown.extend(a)
unknown=','.join(unknown)
bob()
Again, this is basically what is in my code except for the name of the function and the name of the list, other than that it is the same. The name doesn't matter though. Thanks in advance.

Returning Filename with wildcards in VFP 9

I am trying to locate the full name of a file using a wildcard. The code I have is:
MLCNo=crjbis.ffromlot
subfolder=LEFT(mlcno,3)
filename=SYS(2000,'S:\MLC\MLC#\'+subfolder+'xx\'+mlcno+'21391*.pdf')
pathname="S:\MLC\MLC#\"+subfolder+"xx\"+filename
Pathname is passed to a print function to print the file. All of this works great if I don't use a variable in the SYS function (even with a wildcard). I should add that there will only ever be one file returned by the wildcard. Is there another way to do this?
Thanks!!!
Tammy

How do I print colors into the console with D?

I have tried escape sequences with the writeln() function, I also tried to use them with the printf() function imported from the std.c.stdlib module, but it only prints an empty line.
printf("\0x1B[5;32;40m Blink Text");
printf("\e[5;32;40m Blink Text\e[m");
writeln("\0x1b\x5b1;31;40m\tColor");
None of these work.
I have tried everything I can think of, is there a way?
Searching the D website's library reference didn't help me.
EDIT: SOLUTION
Okay, so I have tried to import the function SetConsoleTextAttribute, as Mars kindly suggested:
extern (Windows) bool SetConsoleTextAttribute(void*, ushort);
I also imported the other function (Which I simply guessed I need to import, as I have no previous experience with Win programming)
extern (Windows) void* GetStdHandle(uint);
And simply called the two functions
auto handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_BLUE);
writeln("In Color");
This works perfectly, thank you all so much for your time and help
Like CyberShadow pointed out, you have to use \x1B, or \033. It should work fine, as long as you're on Linux. Windows doesn't support those codes though. Here you have to use the API function SetConsoleTextAttribute from std.c.windows.windows.
There is a typo in your string: use \x1B instead of \0x1B.
D doesn't support the \e escape code in strings, use \x1B.
You may also try a helper module like http://www.digitalmars.com/d/archives/digitalmars/D/Color_your_terminal_s_output_146182.html

Resources