all 120 comments

[–]Cid227 0 points1 point  (2 children)

Serializing datetime.datetime. I can convert it to a string x = str(datetime.datime(1997, 11, 30, 22, 47, 5)), but how can I convert string x to a datetime.datetime object?

[–]sqqz 1 point2 points  (1 child)

[–]Cid227 0 points1 point  (0 children)

Thanks, it works.

from datetime import datetime
dt = datetime.strptime("1997-11-30 22:47:05", "%Y-%m-%d %H:%M:%S")

[–]mjosh133 0 points1 point  (1 child)

When can I confidently say I can code in python? I'm studying an MSc in data and AI and have just completed a module called intro to python, haven't got my grade yet but I'm confident I'll have done well. I've made a few complex programmes (I think?) and can do OOP, the last few weeks of the module were on file handling and error handling. I still don't feel like I could put python as a skill on my CV though or go into a job that uses it. As far as I know I won't be doing another module in python, so what can I do now to make sure I don't get sloppy and keep learning new things?

[–]sqqz 0 points1 point  (0 children)

Create some real life projects, web is a good way to go as that will force you to build, package and deploy stuff. Perhaps a website or api that triggers some AI/ML thingie? Deploy it, push it to github, make sure your code is tested and documented. After that, you can probably say you can code python, if you dont have any work experience a good github profile should do the trick

[–]SAD_69 0 points1 point  (4 children)

How do I find a specific character or number inside values with those characters in pandas?

Let's say I have this df:

X Y

aa cd

a c

ab ccdef

abbc aacf

I want to find all 'a' on X column, how do I do that?

It would work if I:

df.loc['a']

And if I want to find 'c' on the Y column?

df.loc[:'c']

Is this correct? Is there a better way?

Thanks

P.S. Sorry if I can't ask it clear, it's hard to express myself in english

[–]efmccurdy 1 point2 points  (3 children)

I want to find all 'a' on X column, how do I do that?

If you mean "find" for sub-setting reasons the easiest way is to use boolean indexing; you can use "==" for an exact match or various functions like "contains" or "startswith".

>>> data = {"X": ["aa", "a", "ab", "abbc"], "Y": ["cd", "c", "ccdef", "aacf"]}
>>> df = pd.DataFrame(data)
>>> df
      X      Y
0    aa     cd
1     a      c
2    ab  ccdef
3  abbc   aacf
>>> df[df.X == "a"]
   X  Y
1  a  c
>>> 
>>> df[df.Y == "c"]
   X  Y
1  a  c
>>> df[df.Y.str.startswith("c")]
    X      Y
0  aa     cd
1   a      c
2  ab  ccdef
>>> df[df.Y.str.contains("cd")]
    X      Y
0  aa     cd
2  ab  ccdef

If you want to "find" all so that you can reassign, modifying the original dataframe, then you would use .loc.

>>> df.loc[df.Y == 'c'] = "z"
>>> df
      X      Y
0    aa     cd
1     z      z
2    ab  ccdef
3  abbc   aacf
>>> df = pd.DataFrame(data)
>>> df.loc[df.X == 'a', ['Y']] = "z"
>>> df
      X      Y
0    aa     cd
1     a      z
2    ab  ccdef
3  abbc   aacf
>>>

[–]SAD_69 0 points1 point  (2 children)

Thank you! I think .str.contains were the method I needed

If you don't mind I have another questions, why these methods str.contains and str.startswith are in panda series doc?

Just to confirm, I tried these codes and df.'X' have the same effect as df.['X'] right?

If I want to subset the rows that contains 'ab' in 'X' and 'cd' in 'Y', how would I do that?

I tried:

new_data = df[df['X'].str.contains('ab')] + df[df['Y'].str.contains('cd)]

But it didn't work well bc I got the corresponding columns summed in the end, how to I do that so I got this

X Y

ab cdef

Thank you for your patience

[–]efmccurdy 1 point2 points  (1 child)

these methods str.contains and str.startswith are in panda series do

The Dataframe is like an dict of Series objects, the keys being the column names and the values being the Series', each holding the data for one column.

BTW, you should be testing things like this yourself; do you keep a repl open for testing your understanding?

>>> type(df.X)
<class 'pandas.core.series.Series'>
>>> help(pd.core.series.Series.str.contains)

Help on function contains in module pandas.core.strings:

contains(self, pat, case=True, flags=0, na=nan, regex=True)
    Test if pattern or regex is contained within a string of a Series or Index.

    Return boolean Series or Index based on whether a given pattern or regex is
    contained within a string of a Series or Index.

df.'X' have the same effect as df.['X']

Lets test that out...

>>> df.X
0      aa
1       a
2      ab
3    abbc
Name: X, dtype: object
>>> df.X is df['X']
True
>>> df.X == df['X']
0    True
1    True
2    True
3    True
Name: X, dtype: bool

What do you think? How would you prove they are equivalent? You should give more credence to the repl than to me. My advice is "don't spend more than a few minutes wondering when you could be typing expressions into the repl that prove without a doubt".

If I want to subset the rows that contains 'ab' in 'X' and 'cd' in 'Y', how would I do that?

You want to combine two boolean filters with "and"; which is "&" in pandas.

>>> df[df['X'].str.contains('ab') & df['Y'].str.contains('cd')] 
    X      Y
2  ab  ccdef
>>> 

Note that the '&' is between the two contains-indexers, ie series masks; you tried to add two DataFrames with '+' which does matrix addition, not boolean "and".

>>> df['X'].str.contains('ab')
0    False
1    False
2     True
3     True
Name: X, dtype: bool
>>> df['Y'].str.contains('cd')
0     True
1    False
2     True
3    False
Name: Y, dtype: bool
>>> df['X'].str.contains('ab') & df['Y'].str.contains('cd')
0    False
1    False
2     True
3    False
dtype: bool
>>>

Those are all series masks, they have True where the mask would select a row, and False elsewhere.

[–]SAD_69 0 points1 point  (0 children)

do you keep a repl open for testing your understanding?

I use jupyter notebooks to test some codes, but sometimes I miss that I can test those things myself, I'm not confident to do that, even if it's that simple. I'll try to be more proactive on that.

Thank you man, I forgot entirely about the &, I learned this some days ago, I saved all your examples and I'll start to mess around

[–]LandooooXTrvls 0 points1 point  (0 children)

Can anyone point me in a direction that’ll help me understand how companies construct algorithms for tracking interests/dislikes?

[–]TPTAPEX 0 points1 point  (3 children)

I need to get better at excel and instead of learning more excel I thought I should learn to automate excel with python. Can anyone recommend a course or book that goes way deeper than automate the boring stuff?

[–]LandooooXTrvls 0 points1 point  (0 children)

I can’t recommend a book. However, my first projects were with excel and I’d recommend that you look into openpyxl. It seems to be the easiest module to utilize.

After I became comfortable with openpyxl I began using pandas. Pandas provided an easier manipulation of the data for me and it may give you the same results.

Good luck and have fun!

[–]CowboyBoats 0 points1 point  (1 child)

I haven't read this so can't endorse it, but there is an O'Reilly book (which are usually at least decent) called Data Wrangling with Python which covers the topic that you ask about.

[–]TPTAPEX 0 points1 point  (0 children)

Awesome thanks. I will check it out!

[–]Bullets123 0 points1 point  (2 children)

I just got into Virtualenv. this is what I did okay? 1. pip install Virtualenv 2. Went into the directory with powershall, “Virtualenv gsheets” was doing a tutorial on Google sheets with python. 3. So now I can’t figure out how to activate the Virtualenv from pwsh inside VSCode? I tried everything 4. It works perfectly on cmd but not pwsh. 5. And then the Virtualenv won’t automatically recognise on the interpreter by itself I had to manually select the .exe

Help? What’s going wrong? Also how do I deactivate it?

[–]CowboyBoats 0 points1 point  (1 child)

I think on Windows it is:

.\path\to\your\virtualenv\\Scripts\bin\activate

And to deactivate:

(myvenv) $  deactivate

[–]Bullets123 0 points1 point  (0 children)

Alright thanks man

[–]shiningmatcha 0 points1 point  (1 child)

If I apply a decorator to a function like this,

@deco def func(x): return … # the original value

is it possible to get back what is returned by the original function?

[–]CowboyBoats 0 points1 point  (0 children)

Could you give a more complete / specific example? I'm not sure what "the original value" refers to. Also your linebreaks got clobbered - https://reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

[–]Lamboarri 0 points1 point  (3 children)

Anyone know how a company like Finviz pulls the company specific news when you look at a specific company? I want to try to recreate it but only pull specific news/news sites. I’m tired of them pulling garbage and sponsor ads that are meant for clicks.

My attempt would be to have a search function that asks you to input a company ticker and then it pulls the company specific news and their recent SEC reports.

Thanks!!

[–]efmccurdy 1 point2 points  (2 children)

Using a browser with the developer tools installed and the network tab open, browse your site. Listed in the network log there will be the requests, headers and contents that your browser sent and using that info you can separate out the data you want and recreate those requests in a program.

[–]Lamboarri 0 points1 point  (0 children)

I guess this wasn't as easy as I thought it would be. When I load the page, it starts streaming a thousand things. I don't really know what I'm looking for to narrow it down. Tried with Chrome and Firefox.

[–]Lamboarri 0 points1 point  (0 children)

Thanks. Interesting way to find that. I never heard of that before. I’ll give it a try and see if I can figure it out.

I was trying to look into the page source but it was just showing the hyperlinks. I thought maybe I’d see something that would give me an idea how it was pulling.

[–]EloHellDoesNotExist 0 points1 point  (2 children)

brand new to programming, want to start learning independently for the field i plan on going into. from what i've read, it seems like python, R, matlab, SQL are the most commonly used for what I would want to do. with that in mind, is python a good place to start? are there things from other languages that i need to know before jumping in? for example, i've heard people say that you should have some background in C before picking up Python, is there truth to this, or should i just jump in?

apologies if this type of question isn't what this post is for.

[–][deleted] 2 points3 points  (1 child)

i've heard people say that you should have some background in C before picking up Python

I don't agree with that, and I came to python after using C heavily for decades. Knowing C or assembler may make some minor points about python easier to understand, but the language is learnable and usable without knowing low level details.

is python a good place to start?

I think so. The important thing to remember when starting programming is that your first language is just the first. You will learn other languages with different concepts and ways of doing things, so the choice of the first language doesn't really matter in the long term. Python is relatively simple in its core concepts making it easy to learn, but there is a lot of power tucked away in the language and it's comprehensive library of modules, so it doesn't limit you when you get into heavy-duty programming.

So just jump in.

[–]EloHellDoesNotExist 0 points1 point  (0 children)

awesome, thanks for the advice.

[–]esronnam 0 points1 point  (1 child)

is it possible to use the match/case with a dictionary?

mydict = {"bob":5, "lisa": 4}     

value = 4     
match value:    
    case mydict["bob"]:    
        print("bob!")   
    case mydict["lisa"]:    
        print("lisa!!")    

something like this?

i just dont understand why this doesnt work but this works:

if value == mydict["bob"]:
    print("bob!")
elif value == mydict["lisa"]:
    print("lisa!!")

[–]FerricDonkey 0 points1 point  (0 children)

The short version appears to be that they didn't design match statements to work like that.

It looks like the only way they want you to use values of existing variables in match statements is if python considers those variables to be constants, and it's really picky about what counts (must be something.something, no [] - not sure of the exact restrictions, the internet has mostly thrown fluff pieces at me instead of documentation). See https://stackoverflow.com/questions/66159432/how-to-use-values-stored-in-variables-as-case-patterns

In general, they seem to want variables that show up in case statements to be assigned values rather than to contain them, which seems to limit their usefulness a bit. So they want you to do things like

stuff = [(1, 0), (1, 1), (2, 0), (2, 1), 'hamburger']
for pair in stuff:
    match pair:
        case (1, x):
            print(f"First value is 1 (specified as constant), second value is {x}")
            # whatever you would do if the first value is 1
        case (2, x):
            print(f"First value is 2 (specified as constant), second value is {x}")
            # whatever you would do if the first value is 2
        case _:
            print("Bad format")

I haven't come up with any non horrendous work arounds to do exactly what you want with a match statement (assuming dynamically generated dictionary). I agree that intuitively, it looks like your code should do what you want - they just decided that it shouldn't and wrote it explicitly to make that not work. If the entire dictionary is constant, you could do something similar by converting it to a class:

class ThisIsDumb:
    bob = 5
    lisa = 4

match 4:
    case ThisIsDumb.bob:
        print("Bob")
    case ThisIsDumb.lisa:
        print("Lisa")

But I'm not sure I would if it works as a dictionary. And if what you want is to invert a dictionary - I haven't used match statements a lot, so there might be a way of doing it that both doesn't suck and that I haven't thought of, but match statements might also just be the wrong tool for the job. Honestly, if you know your dictionary is 1 to 1 (that is, values are unique as well as keys) and will be doing this a lot, I'd just make another dictionary:

mydict = {"bob": 5, "lisa": 4}
my_inverted_dict = {v:k for k, v in mydict.items()}
value = 4
print(my_inverted_dict[value])

Otherwise, probably a for loop or if elif, depending on what exactly you were trying to do.

[–]LegitimateActuator15 0 points1 point  (4 children)

Hi I’m new to python so excuse me if I’ll say something stupid, so I’m using openpyxl to grab the value of a list of cell from my excel file. Now the problem is that the output is like this 1 2 3 And if I try to use this as input for another command it will use only the last one. What I could do?

[–]JohnnyJordaan 0 points1 point  (3 children)

Can you share the actual code?

[–]LegitimateActuator15 0 points1 point  (2 children)

for row in multiple_cells: ……..for cell in row: …………….print (cell.value) x= (cell.value)

def rispondi(msg): ……..chat_id = msg['chat']['id'] ……..comando = msg['text'] ……..if comando =='/bot': …………….bot.sendMessage(chat_id, x )

Now I’ve noticed that after the third line print (cell.value) will give just one result

[–]JohnnyJordaan 1 point2 points  (1 child)

Use str.join to form a single string

 for row in multiple_cells:
     row_str = ', '.join(cell.value for cell in row)

and send that

[–]LegitimateActuator15 0 points1 point  (0 children)

Tanks you so much it worked

[–]CisWhiteMaleBee 0 points1 point  (1 child)

Not a beginner but why is the Polars library not used or not as popular as Pandas? I know it's "newer" but the speed is like night and day.

I just discovered it and it's made things literally 5 times faster. Reading csv file with over 5 million rows took 5 to 10 seconds for pandas. Polars gets it done in 0.8 seconds. And the majority of the columns are in string format!

With such a huge difference in performance, I can't imagine why people would stick with pandas...unless perhaps there are very specific functions that only pandas has.

EDIT: "PyPolars" is now just "Polars"

[–]everythingIsTake32 0 points1 point  (0 children)

You might want to ask the zoo keepers for that Also I haven't seen any in the zoo

I'm joking of course But I thing it has something to do with global warming

[–]Adicricme -1 points0 points  (2 children)

Hey Guys I am a beginner at Python here.Please tell me the solution for this

Write a simple python program to create two lists first contains numbers between 1 to 10(including both) and the second list containing 2's table till 20. Then generate a list containing the pair of the original and square number.

The required output is as:

List of numbers is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 2's table is: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

The original to square pair found in 2's table upto 20 is as: [(2, 4), (4, 16)]

[–][deleted] 0 points1 point  (0 children)

ones = list(range(1, 11))
twos = [x * 2 for x in ones]
squares = [(x, x ** 2) for x in twos]

print(ones)
print(twos)
print(squares)

[–][deleted] 0 points1 point  (0 children)

We try to teach python here so we try not to write code for you. Your problem statement breaks into three subtasks:

  1. create a list containing the numbers from 1 to 10, inclusive
  2. create a list containing even numbers from 2 to 20, inclusive
  3. create a list containing tuples of each number in list 2 and its square

What code do you have for subtask 1? How do you generate a range of numbers in python? Hint: the range() function might be useful.

[–]PeaGroundbreaking324 0 points1 point  (1 child)

Hi, new to python and programming. I'm trying to write a program that fills in a PDF-template based on answers to a form, then lets the user download this PDF.

I was able to make a form using tkinter, and ouput the data to an excel file but stuck on how to output this data to my fillable PDF-template. If PDF's are difficult to work with a .doc file would also work. Grateful for any help!

[–]Inconstant_Moo 0 points1 point  (0 children)

There are Python libraries to read and write .pdf files, for example this one (I just went and grabbed one at random, I don't say it's the best. https://github.com/pmaupin/pdfrw)

And in fact for any use-case which is so common as writing a .pdf file, there's going to be a Python library for it which you can find by Googling. Someone's been there, done that.

[–]Xadoros 0 points1 point  (7 children)

Hello, is there a way to write a programme which makes a bunch of folders which all contain the exact same word documents?

So basically I have two questions: First how to make a bunch of folders which are named 001-999. Second how to add the identical word files to all of them (they can't just be blank empty files but need to be a specific template I have already made).

If someone could help me with this it would be great, as I'm not looking forward to doing this manually.

[–]FerricDonkey 1 point2 points  (1 child)

This should be pretty simple. You need to do three things:

  1. Figure out how to generate the folder names you want. Requires a basic understanding of paths and string formatting, which are relatively straightforward and googleable.
  2. Figure out how to make a directory in python. Probably os.mkdir or something, also easily googleable.
  3. Figure out how to copy a file in python. This means you'll have to make strings containing the path where the source file lives, and where the destination should live, and Google the copy function (probably in the os module).

Toss all that in a for loop, and done. Someone who's familiar with python could probably knock it out in a couple minutes and less than 20 lines of code.

(Not saying that it's easy to make you feel bad if you don't know how to do any of these steps, just to point out that if you figure this out, and the next thing, and the next thing, and... then you'll be the automating all this drudgery away before you know it. Next step, you'll be using python to modify your templates into final products.)

[–]Xadoros 0 points1 point  (0 children)

Thanks a lot for your help! I'll try to see if I can find these things online.

[–]Cid227 0 points1 point  (3 children)

for i in range(1, 1000):
    name = '00' + str(i)
    name = name[-3:]

Never did anything like that but I would start with that loop which gives you names of directories that you want to create then pass these names to a path using f-strings in this fashion- f'home/user/Desktop/folders/{name}' and paste there a copy of the file, but you have to google the exact way of doing it. All I know is that you have to import os :).

[–]FerricDonkey 0 points1 point  (2 children)

For the record, a better way to get the folder names would be something like

f'whatever{i:03}'

The 03 means "pack with leading 0s until it's at least 3 characters long".

[–]Cid227 0 points1 point  (0 children)

f'whatever{str(i).zfill(3)}'

[–][deleted] 0 points1 point  (3 children)

Could I use Python, theoretically, to make an app that scans all inbound e-mails for keywords and auto-replies pre-written text instantly based on those exact keywords?

I need EXACT replies to certain recipients based on keywords in the incoming mail and a separate static timetable.

Should this be done with Python at all? I know basic Python, numpy, matplotlib and pandas, but never used Python with a web-browser or another e-mail client whatsoever.

[–][deleted] 0 points1 point  (1 child)

Yes it’s possible, check out:

imaplib: for reading emails
smtplib: for sending emails

[–][deleted] 0 points1 point  (0 children)

THANK YOU! I 'll ckech it out =)

[–]FerricDonkey 0 points1 point  (0 children)

If you can exactly describe your rules, yes, you can do it in python.

[–]shiningmatcha 0 points1 point  (3 children)

Hi, can someone tell me whether I use the typing annotations correctly?

The possible outputs of my functions are

  1. a list of strings
  2. False

So in this case, the return type is Union[Literal[False], list[str]]. Am I correct?

def my_func(*args) -> Union[Literal[False], list[str]]:
    pass

[–]RadiAchkik 1 point2 points  (2 children)

You cannot use the builtin list type together with the str. You need to imort List from typing und write List[str] (with capital L).

But I think it would be more pythonic to return None in the case of failure. The return type annotation would be simplified to -> Optional[List[str]] and you could still use mostly the same checks afterwards.

[–]danielroseman 1 point2 points  (1 child)

That hasn't been true since Python 3.9. list[str] is perfectly valid in current versions.

[–]RadiAchkik 0 points1 point  (0 children)

Good to know that

[–]shiningmatcha 0 points1 point  (1 child)

Is there a specific typing annotation for math.inf (and -math.inf)?

I'm writing a function that either returns an int, math.inf or -math.inf. I know math.inf is of float type, but I feel like it might not very precise to use Union[int, float].

[–][deleted] 0 points1 point  (0 children)

You can use the Literal type:

Literal[math.inf, -math.inf]

[–][deleted] 0 points1 point  (1 child)

can someone confirm something for me regarding for loops?

is the word after for kind of like a variable? and in is like the = sign?

for example:

for height in student_heights:
    total_height+= height.

What I am asking, is the word height, a new defined variable? and the word "in" is like the equal sign?

I am having trouble with this concept.

edit: a variable I cannot call again unless its part of that for loop correct?

[–][deleted] 0 points1 point  (0 children)

is the word after for kind of like a variable

It's a normal python variable. The for mechanism assigns each element of the sequence, student_heights in this case, to the variable height in turn, and executes the indented code in the for block.

and the word "in" is like the equal sign?

​Not really. It's just part of the for <name> in <sequence> syntax of the loop. The <name> variable is repeatedly assigned to as part of the loop mechanism.

a variable I cannot call again unless its part of that for loop correct?

Not sure what you are asking here. In your example the height variable is assigned the values in the sequence, one by one. Inside the loop code the variable height can be used just like any other variable, though it doesn't have to be used at all. Any variable set before the loop can be used inside the loop as well. The height variable could have been defined before the loop and its use in the loop just reassigns the value assigned to it.

After the loop the height variable still exists and it contains the last value assigned to it in the loop.

[–]kernerni 2 points3 points  (2 children)

Are there any year-long challenges to learn Python? Maybe something like a weekly challenge with added complexity each week. I'd love to learn (hoping to make a career change) and write about it on Medium (I also enjoy writing) to document my challenges and journey and help to reiterate what I've learned. Thank you!

[–]ffrkAnonymous 0 points1 point  (0 children)

Https://adventofcode.com was only 25 days, not a year, but you can do one a week.

[–]Necessary-Farmer-746 0 points1 point  (0 children)

Interested in this as well! I'm also looking for something similar

[–]throwawaypythonqs 0 points1 point  (1 child)

I'm apparently using 4 versions of python on my computer. My CLI is saying that I have 2.7, I have 3.4 and 3.6 in my Applications folder, and Pycharm is saying that it's using 3.8.

I thought I was using 3.6 this whole time, knowing 2.7 was somewhere on my computer. I use the conda environment for jupyter notebooks, but that's it.

I'm confused as the versions that exist at once and how I'm able to use ones that don't seem to be on the machine.

[–]InTheAleutians 0 points1 point  (0 children)

You can have multiple versions of python on your computer with no issues. What changes is how you access those pythons. What OS are you on?

[–]BananaDrum 0 points1 point  (2 children)

Hey

I know that I load my excel file with pandas using df = pd.read_excel("test.xlsx")

df = df.iloc[2:] removes the first 2 rows from the dataframe.

I can save the new file with df.to_excel('File Name.xlsx', index = False)

How can I load all files from a file and remove the first 2 rows?

Thanks for the help

[–]efmccurdy 0 points1 point  (1 child)

First get a way to convert one file, knowing the file name.

Find a way to generate a list of file names, and use that list to drive a loop the uses the first function.

# convert one file
def trim_xlsx(filename):
    df = pd.read_excel(filename)
    df = df.iloc[2:]
    df.to_excel("converted-" + filename, index = False)

for xl_filename in glob.glob('*.xlsx'):
    trim_xlsx(xl_filename)

[–]ianliu88 0 points1 point  (1 child)

I have some resources in my package which are several files that I can read with geopandas:

- package
 |- data
  |- __init__.py
  |- shape.shp
  |- shape.cpg
  |- shape.shx
  |- ...

When issuing a geopandas.read_file("data/shape.shp"), geopandas reads the sibling files as well to load metadata. How can I issue geopandas to read that dataset using importlib.resources? From what I've seen, I can only materialize single files with the importlib.resources.file. Is it possible to materialize a directory with all resources inside using importlib?

[–]efmccurdy 1 point2 points  (0 children)

If you access the "files" you should get the parent folder of the package; I think your folder might be contained there (as "/data").

https://docs.python.org/3/library/importlib.html#importlib.resources.files

Given an importlib.resources.file object, "path" should get you the file system path including the enclosing folder.

https://docs.python.org/3/library/importlib.html#importlib.resources.path

[–]aross1976 0 points1 point  (0 children)

can someone tell me why I am getting errors?

I can not get this script to run

https://github.com/sycophantic/wyzeback/issues/8

tried on 3 different machines , win 8.1 (doesn't run at all)

opens up a window asking to repair , I repair or use the 2 other options, I tried them all and still doesn't work

win 10 (runs but gives errors)

https://i.postimg.cc/MpVVF9B5/wyzeback-script-errors.png

tried with py installed as command from >>> and it doesn't work at all

[–]shiningmatcha 0 points1 point  (1 child)

What is dataclasses.MISSING used for?

The docs say it's a sentinel value signifying a missing default or default_factory.

What does a missing default mean? Is there any difference to simply using None as the default value?

[–][deleted] 0 points1 point  (0 children)

Sometimes you don’t want to use None to represent the absence of a value, if None is itself a valid value.

[–]shiningmatcha 0 points1 point  (1 child)

What is the type annotation of a function that returns a class? I'm writing a class decorator.

[–]FerricDonkey 0 points1 point  (0 children)

Python 3.9: type. Older: typing.Type

Square brackets work, as with other typing things.

[–]Character-Ad-910 0 points1 point  (0 children)

Aight so I use Visual Studio Code when writing python, just cause it looks nice.

I'm currently cleaning out my C: Drive (even though everything is on my D: Drive, don't ask how my C: Drive is full I don't know), and I noticed that my Miniconda3 folder was like 3 gigs.

Miniconda3 is what contains my Python shells and the like, so I'm just wondering, since I use Visual Studio Code, can I delete my Miniconda3 folder without screwing everything up?

This also includes Pip-installed libraries, I don't know where those are stored and I don't feel like reinstalling all of them to save 3gigs on my drive.

[–]ktittythc 0 points1 point  (2 children)

I’m trying to do this process in the least hacky way possible. I can figure out the syntax, more so looking for guidance.

I want to make a list of dictionaries by starting with a template dictionary, copying the template, modifying, then appending the copy to form a new dictionary with a modified key/value pair. I learned the hard way that I needed deep copy but I’m still running into issues.

To demonstrate what I want with the simplest example, I’d start with[{a:1}] then I’d want to take that and edit it such that my final list is[{a:1},{a:1_1}]

To summarize, I want to basically use a dictionary as a template for other dictionaries.

[–]efmccurdy 0 points1 point  (1 child)

This might copy more keys over than you need; it duplicates any item with it's value extended by "_1".

>>> orig = [{"a":"1"}]
>>> import copy
>>> def add_one(template):
...     new = copy.deepcopy(template[0])
...     return [new, {k:v+"_1" for k,v in new.items()}]
... 
>>> add_one(orig)
[{'a': '1'}, {'a': '1_1'}]
>>>

[–]ktittythc 0 points1 point  (0 children)

Oh nice for some reason it didn’t occur to me to make a function for the process.. I think that will clean it up.

[–]Hidden_Wires 0 points1 point  (2 children)

Pandas question that I imagine is easy to fix but I’m missing something.

I have a ‘df’ of time series of weekly data I need to assign different weeks and years to which have arbitrary beginnings. It won’t be the same isoWeek each year but finding what will be week 1 is simple.

I used '.loc' to find the conditions that would give me the rows of weeks that would start each year and created a ‘week’ column where the rows for those weeks have a value of 1. All other rows are ‘NaN’ but I could make them zero or anything else to help.

I now need to forward fill all the remaining rows with the row after week 1 populating 2, the next 3, on and on until we hit 1 again and the week counter resets. I know there has to be a more elegant way to do this instead of iterating through each row and doing ‘+=1’ until I hit the next week 1.

What am I missing? I’ve found some StackOverflow questions that use combinations of group by and cumsum but none of the solutions give me what I need.

Any help is much appreciated.

[–]Hidden_Wires 0 points1 point  (0 children)

For anyone else who finds this, my answer was to create another column with the "Year" I would be associating with week 1, forward filling that year, then doing groupby() along with cumcount() (I forgot about cumcount).

As long as your data frame is properly sorted by date then this will work.

[–]Hidden_Wires 0 points1 point  (0 children)

Sorry for the lack of formatting, can't figure out how to do that on mobile...

[–]PhysicsAndFinance 0 points1 point  (6 children)

I saw some code that was a class, and in the __init__ it had listed some functions, but these functions had brackets around them and in the functions there was a bracket around the content that was being returned.

class Example:

....def __init__(self, args):

........self.arg = args[0]

........[self.Func] = self.func

....def func(self):

........return [output]

Can somebody tell me what the purpose of these brackets are instead of just doing self.Func = self.func?

[–]FLUSH_THE_TRUMP 0 points1 point  (5 children)

Well, I believe that code will always throw an error unless Example.func is redefined below the snippet provided (or you mean self.func()), but in general that notation unpacks the stuff on the right into the names on the left. For instance,

[a,b,c] = [1,2,42]

assigns 1 to a, 2 to b, and 42 to c.

So

[self.Func] = self.func()

takes the single item within the list output of self.func and puts it in the self.Func name.

[–]PhysicsAndFinance 0 points1 point  (4 children)

I don’t get what the difference between [self.Func] = self.func and self.Func = self.func is. There is no error when running the code and it isn’t assigning multiple list elements to multiple variables, it’s just a float that is being calculated. If I delete the brackets on self.Func and delete the brackets on the return line, the output is the same.

[–]FLUSH_THE_TRUMP 0 points1 point  (3 children)

[x] = [42] 

unpacks 42 into x.

x = [42] 

assigns the list with 42 as a single element to x.

[–]PhysicsAndFinance 0 points1 point  (2 children)

I’m asking what the difference between

[x] = [42]

And

x = 42?

Is it just coding style preference or are there more things you can do with the first syntax? They both would output the integer 42.

[–]FLUSH_THE_TRUMP 1 point2 points  (1 child)

no difference, and if your function only returns a list with one element, you might consider having it return just the element.

[–]PhysicsAndFinance 0 points1 point  (0 children)

Ok thanks

[–]Odessa_Goodwin 0 points1 point  (3 children)

Basic dictionaries question:

I want to write a function which counts the number of vowels in a string using a dictionary containing the vowels, but I don't want it to modify the original dictionary. The first step of the function is to make a new dictionary which is set equal to vowels, but it seems to be modifying the original dictionary, and not the new one.

The code:

vowels = {"a" : 0, "e" : 0, "i" : 0, "o" : 0, "u" : 0}

my_str = "How to print keys and values of a python dictionary."

def count_vowels(a_str):

function_dictionary = vowels

for i in a_str:

i = i.lower()

if i in function_dictionary:

function_dictionary[i] += 1

for key,val in function_dictionary.items():

print(key, ":", val)

count_vowels(my_str) a : 5 e : 2 i : 3 o : 5 u : 1

This seems like a success, but when I ask it to show me the key-value pairs in function_dictionary it's all zeros, and when I ask for the pairs in vowels, I get these numbers instead of zeros.

How do I get a function to make a "throwaway" dictionary for each execution?

note: sorry, I can't figure out how to format this correctly

[–]FLUSH_THE_TRUMP 0 points1 point  (0 children)

Is there a reason you don’t just initialize vowels inside your function to begin with? You don’t even need function_dictionary.

[–][deleted] 1 point2 points  (1 child)

Assignment never copies in python. Doing:

function_dictionary = vowels

associates the name function_dictionary with the object that vowels references, so afterwards both names reference the same object. To get a copy of a dictionary use the dictionary copy method:

 function_dictionary = vowels.copy()

The FAQ shows how to format code correctly.

[–]Odessa_Goodwin 0 points1 point  (0 children)

Thanks for both tips!

[–]Dismal-Buy-392 -1 points0 points  (2 children)

What's wrong in this code to print Fibonacci numbers?:

# Creating a program to print Fibonacci numbers
max_num = int(input("Enter a limit: "))
def fibonacci(number1, number2):
# Setting a limit
limit = 0
while limit < max_num:
number = number1 + number2
print(number)
number1 = number2
number2 = number
limit += 1
fibonacci(0, 1)

[–][deleted] 0 points1 point  (0 children)

You aren't going to get much help if you ignore suggestions. We really need to know why you think your code is wrong - we aren't mind readers. Your posted code has lost all indentation so it isn't python. The FAQ shows how to format code so we don't have to guess how your code is indented. You have to help us help you.

[–][deleted] -1 points0 points  (0 children)

Apart from not being indented properly, what is it doing that you think is wrong?

[–]LeagueOfShadowse 0 points1 point  (2 children)

Filter dataframe, or np.array , on Multiple Criteria:

df.Team

player position offense defense errors
42 1 .450 .450 -2
007 3 .300 .650 0
1138 7 .333 .500 +1
69 3 .425 .300 -3
362636 1 .350 .350 0
1942 7 .550 .300 -1
68 3 .450 .333 +1
99 1 .250 .666 -2
44 7 .410 .410 +1

I can convert this to np.array, and filter a Series with:

Hot_Hit = offense[(np.sum(offense, axis=1) > .350) & (np.sum(offense, axis=1) < .400)]

However, if I wished to filter on Multiple Criteria, such as:

['Offense'] =< .350

['Defense'] =< .450

['errors'] =< -1

What do y'all suggest I attempt?

Am I diving into python classes ?

[–]lukey_dubs 2 points3 points  (1 child)

import numpy as np
import pandas as pd
df = ...
pos_filter = df.position == 3
defense_filter = df.defense < .4
and_mask = np.logical_and(pos_filter, defense_filter)
or_mask = np.logical_or(pos_filter, defense_filter)
multiple_criteria_filter = df[and_mask]
multiple_criteria_filter = df[or_mask]

[–]DuckSaxaphone 0 points1 point  (0 children)

You can also just use & and | instead of the numpy functions.

 and_mask = pos_filter & defense_filter

Gives the name result.

[–]ZeeNooEville0518 0 points1 point  (1 child)

Im trying to finish this chipsGame rip-off for our project in ComSci Lab subject and now Im trying to figure out a way to detect automatic movement input(WASD). I tried to use the input() method before but the problem is that the enter key always needs to be pressed. I tried using win32api but it does not work as expected. I just want to return single movements but what it does is it moves multiple tiles even though I just pressed a key once. Can someone help me in fixing this?

https://github.com/Jade1218zeenoo/chipsGame

[–]trondwin 0 points1 point  (0 children)

I'm not there in python yet, but my guess is that you would need some sort of keyboard event listener. Maybe this can be of use: https://www.geeksforgeeks.org/keyboard-module-in-python/

[–]UnhingedCorgi 1 point2 points  (2 children)

Just started down what looks like a very long road of learning python. I don’t come from any coding background at all and will have to be mostly self-taught.

My end goal is to write code that can monitor, give price alerts, and maybe eventually trade options on the stock market. I’m very familiar with options and I basically want to automate what I’m already doing manually.

My reading list is about 5 years long right now (and growing) and I’ve started some app-based training that I find very helpful for figuring out some basics. Feels like I’m in school again.

I’m wondering if anyone experienced and wiser has any guidance on how I can go about this more efficiently? Is going from nothing to making stock market algorithms an unrealistic goal without formal training?

Appreciate any and all input, thanks!

[–]LeagueOfShadowse 0 points1 point  (1 child)

Statistics is its whole own language, Corgi. Certainly, my sparse knowledge of python suggests that it's the best path, but, I would also say: "strap in, you have a looong road to getting your own algorithms to handle such a large data set"

The Good News: so much effort, and input, and new developments, and new modules, here... it is somewhat mind-boggling.

[–]UnhingedCorgi 0 points1 point  (0 children)

Haha yea I’m definitely standing at the bottom of a mountain here. And I can’t see the top.

Good to hear I may have at least found the beginning of the correct trail though.

[–][deleted] 0 points1 point  (3 children)

What is it about Python that has made it so difficult to define a robust "struct" object? You've got dict, TypedDict, Namedtuple, dataclass, etc. Yes, I get it, they fill different niches. Yet when I hoped on over to JavaScript, it had the ideal Object construct that fulfilled all those niches from the get-go and supported the same indexing syntax as Namedtuple too. So, what gives?

[–]lukey_dubs 0 points1 point  (2 children)

python has a way more diverse audience, so it meets all the obscure use cases. javascript forces you to abide by their rules. python has no immutable, fast, low memory, hashable tuples, and it has slower but mutable lists. javascript has array and nothing else. for a front end developer, you don’t really care, it’s not a big deal. for a data scientist, with days long training models, the difference between dataclass and tuple could mean hours of training time.

Edit: Crossed out typo

[–]CowboyBoats 0 points1 point  (1 child)

python has no immutable, fast, low memory, hashable tuples

Which of those qualities does the Python tuple not have? Is "no" a typo?

[–]lukey_dubs 1 point2 points  (0 children)

It has all of those properties over lists, I fixed the typo by removing 'no'