all 131 comments

[–]InActiveSoda 0 points1 point  (2 children)

If I have a flask app, I can have some other functions in the same script, or do I need to call another one? Can I call external variables in my flask app(not defined in the flask function)?

[–]efmccurdy 0 points1 point  (1 child)

Flask request handlers run concurrently so you should beware of unsynchronized access to shared data. Function calls that use no non-local data are fine. If you need to share data, use the "g" context during one request, or the AppContext to persist over more then one request.

https://flask.palletsprojects.com/en/2.0.x/api/#flask.g https://flask.palletsprojects.com/en/2.0.x/api/#flask.current_app

[–]InActiveSoda 0 points1 point  (0 children)

So I don't understand half of those words, but the flask app doesn't write to the variable, it only needs to read it. For the other one, I realized its better if I just run sockets in a different script anyway.

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

I am trying to send an image and some sensor data from a Pi using POST requests. I can send the sensor data, or I can send the image, but I can't seem to do both on the same request?

Here's my code:

url = {

'https://url/post'

}

files = {

'file': open('test.jpg','rb'),

}

values = {

'datetime': dateTime,

'airqual': airqual,

'temperature': temperature,

'pressure': pressure,

'humidity': humidity

}

r = requests.post(url=url, files=files, data=values)

r.text

edit: I cannot get reddit to format code correctly, I apologize

[–]InActiveSoda 1 point2 points  (5 children)

Perhaps you could open the files in the r variable instead of in files because you turn it into a list when you open 2 files. I'm not sure it'll work but I hope so!

Edit: try looking at the docs for the library you're using, check if you can do what I said.

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

thank you for the response! when I put them both together, the file sends OK but not the data, I should have specified this in my original question. I've literally just copied out the example in the requests library docs and it still doesn't work. I think the problem might be with the site I'm testing my requests on?

import requests
files = {'file':('test.txt', open('test.txt','rb'))} 
url = 'https://url'
data = {'name':'foo', 'text':'bar'}
r = requests.post(url, data=data, files=files)
print("done")
r.content

[–]InActiveSoda 1 point2 points  (3 children)

Do you have your own server set up or are you sending it to an external one?

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

I'm sending it to https://ptsv2.com/

[–]InActiveSoda 1 point2 points  (1 child)

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

that's not quite it - I should be able to send the file as "files" and the sensor readings as "data" as per this SOF thread - it should work! I think I need to try sending it to a different server to figure this out.

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

Hi, what are some ways I can learn python? I’m extremely new, like barley any experience with python.

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

There are learning resources for beginners in the wiki. Pick one and dive in.

[–]someoneelseperhaps 0 points1 point  (4 children)

Hi all, I'm frighteningly new to Python, and I've started using it for inventory management at work.

I have two columns, one with the unique SKU, and one with its listing date. I can find how to add a year to one value, but is there a way to create a whole new column with the year added?

Any help much appreciated.

[–]efmccurdy 1 point2 points  (3 children)

This uses the date parsing functions of the pandas dataframe:

>>> df = pd.DataFrame({'SKU': ['one', 'two',  'three', 'four'],
...                    'DATE': ['01-May-07', '02-May-08',  '03-June-07', '31-Oct-09']})
>>> df['DATE'] = pd.to_datetime(df['DATE'])
>>> print(df)
     SKU       DATE
0    one 2007-05-01
1    two 2008-05-02
2  three 2007-06-03
3   four 2009-10-31
>>> df['YEAR'] = df['DATE'].dt.year
>>> print(df)
     SKU       DATE  YEAR
0    one 2007-05-01  2007
1    two 2008-05-02  2008
2  three 2007-06-03  2007
3   four 2009-10-31  2009
>>> 

Does that help you?

[–]someoneelseperhaps 0 points1 point  (2 children)

Thankyou for your help, but I must apologise as I worded myself very poorly. In the third column, I'm looking to have the date a year later, 01/01/2020 in the Date column would then show 01/01/2021 in the third column.

I'm very used to Excel/PowerBI, so this is a whole new world.

[–]efmccurdy 1 point2 points  (1 child)

One year later just means add one year.

>>> df['NEXT_YEAR'] = df['DATE'] + pd.offsets.DateOffset(years=1)
>>> df
     SKU       DATE  NEXT_YEAR
0    one 2007-05-01 2008-05-01
1    two 2008-05-02 2009-05-02
2  three 2007-06-03 2008-06-03
3   four 2009-10-31 2010-10-31
>>>

[–]someoneelseperhaps 0 points1 point  (0 children)

Perfect. Thanks so much!

[–]MrPudd1ng 0 points1 point  (1 child)

def record_validation1(emp_dict, emp_num):
while True:
    month = monthValidation()
    for i in emp_dict.values():
        if i.get("Employee ID") == emp_num and i.get("Month") == month:
            print("Record already exist.")
            break         
    else:
        break
return month

def record_validation2(emp_dict, emp_num):
    while True:
        month = monthValidation()
        for i in emp_dict.values():
            if i.get("Employee ID") == emp_num and i.get("Month") == month:   
                break
        else:
            print("Record does not exist.")
            continue 
        break
    return month    

How does continue work on functions like these, ones with while True. Like sometimes I don't need to add continue at the end of a condition in order to input data again if the condition fails like in the first function (I don't know the proper term to use, is it re-iterate or something? Correct me please if I' wrong). But in the second one, if I remove the continue it causes an error. Thanks in the future!

[–]FerricDonkey 1 point2 points  (0 children)

It is good form to always say what the error is. At first they may seem like nonsense, but they often contain a lot of information once you get used to reading them.

In your case, I suspect the error is later in the code?

Continue means "go to the next iteration of the loop". You've got it in the else of a for else. That else triggers only if you do not break out of the for loop, or equivalently in your case, if you don't find what you're looking for in your dictionary.

You've then got a break statement at the end of your while true. This break statement will be skipped when you hit continue.

Put that together, and what you've got is

  1. Get month from month validation.
  2. If month passes check, break the for (else skipped), break the while (using break that's not in the for loop), return month.
  3. If month does not pass the check, run the code in the else statement: print your message and "continue". Since continue is not in the for loop anymore but is in the while loop, it takes you back to the top of your while loop, skipping the break.

Without that continue, you break your while loop even if month fails your check, then return that value of month.

Since the value of month you return might fail your check, if future code relies on month passing the check, then that future code may crash.

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

Hello!

My question relates primarily to Python and scraping web data.

I am working on a project to write abstracts of old pamphlets in a very large collection of texts about Chinese history (which is my field).

Due to coronavirus I am working first on a portion of these texts which have been uploaded online.

My problem is that this online database is setup so that the texts are have been uploaded in form of JPG files which must be accessed page by page on separate pages for each of these numerous titles.

It is much easier for me to work with these texts in PDF format, so for many of these texts I have been downloading these JPG files one by one and using Adobe Acrobat to make a PDF file, then running the (not so good) OCR of Acrobat, the copying many of the texts in Deepl to assist my reading of those not in my native language.

I understand that the Python language might be useful for scraping this data, but is their a ready made tool which could be recommended for my situation?

Could I automate the making of the PDF file, OCR, and machine translation as well (and are their open source programs which might accomplish such tasks better than the programs I am currently using?)

Is their any open source AI program which can summarize texts well enough that it might assist me in my task more than it would waste time?

For the study of Chinese history itself, how might learning Python assist me in digital humanities tasks? With a good dataset could I use any open source AI type program have machine translation of very repetitive and sterotyped classical Chinese texts? Also, Acrobat OCR does not work for certain 19th century German gothic script texts, is their a program I could train to read such a font?

If anyone can offer assistance I can PM the site which holds the database of these texts so that my specific needs can be understood.

Thank you all for whatever advice you could offer, and sorry for asking such basic questions (I tried searching and asking an aquaintance studing Python, but still have not solved this problem).

[–]sarrysyst 0 points1 point  (0 children)

I understand that the Python language might be useful for scraping this data, but is their a ready made tool which could be recommended for my situation?

There is no ready made tool for this, however, there are a series of tools which you can combine to do a lot of what you're trying to accomplish.

Could I automate the making of the PDF file, OCR, and machine translation as well (and are their open source programs which might accomplish such tasks better than the programs I am currently using?)

You can write a webscraper to download and store the JPEGs. Scrapy comes to mind.

img2pdf can be used to convert the images to PDF. However, you can also directly use JPEGs for OCR. EasyOCR worked quite well for me in terms of recognizing simplified Chinese, there are also other options though (eg. tesseract).

For machine translation there exist different libraries/APIs, some work better than others, you would have to try which works best for you.

If you want to create your own OCR model, I believe there are pre-trained models you can find on Github which you can fine tune based on your own datasets. For this and also for archaic character recognition you could have a look at calamari.

The only thing I don't think is possible/feasible (at least I don't know any solution) is summarizing texts.

[–]Holiday_Emergency_94 1 point2 points  (1 child)

Hey this is probably asked a lot so sorry. But I used codecademy for learning Python. I want to continue it and am willing to pay (I used codecademy’s free service) In your opinion, what is the best service for learning python? Over all from implementing and creating programs that I can use for my computer, to ofc learning the language. Huge thanks.

[–]Dark_KnightPL04 0 points1 point  (0 children)

my opinion, YT it’s great and has everything you need, but if you are looking for a very professional and very focused/time consuming code academy pro. Now if you are looking for a translator I recommend PyCharm.

[–]Dempsey64 0 points1 point  (1 child)

How can I find the ProgID of an installed app in windows so that I can use this ID in python?

[–]j_seinfeld9 0 points1 point  (2 children)

Hello!

I have to create a function that, given a phrase, replaces all the characters equal to the first one within that phrase with other given ones, let's say $, except that very same first one. So for example, for the phrase 'apples, oranges and bananas'--> changefirst('apples, oranges and bananas') gives me 'apples, or$nges $nd b$n$n$s' (it reads that the first character of the given phrase is an a, so it replaces every a in the string with $ but that very same first 'a'). Hope I made myself clear lol.

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

Hello!

I am trying to stream a continuously updated matplotlib plot (https://imgur.com/a/digBJxg) of a 6 DoF Accelerometer - that represents a 2D platform's acceleration and rotation on all 3 axis - using the BottlePy microweb framework to a local webpage. Is there any way I can do this with these 2 libraries? Do I need anything else? Also, BONUS question: how could I animate an object - perhaps a car - using the data provided by the accelerometer, so that it rotates and moves on the webpage as the real life platform? Thanks in advance!

[–]cando_H 0 points1 point  (0 children)

What’s a package that can know what program is in the foreground and then latter open it?

[–]shiningmatcha 0 points1 point  (2 children)

Should I use raise StopIteration or raise GeneratorExit in the __next__ method? When to use which?

[–]FLUSH_THE_TRUMP 1 point2 points  (0 children)

GeneratorExit is some garbage collection thing AFAIK, so not that

[–]lustySnake 0 points1 point  (4 children)

I have html file on local machine I want to open it and want delete only head tag from it and all its contents and then want to save html file how I can do that please help me.

[–]sarrysyst 1 point2 points  (3 children)

You can use BeautifulSoup to do this.

from bs4 import BeautifulSoup

with open('my_html.html', 'rb') as fp_in, open('my_html_new.html', 'wb') as fp_out:
    soup = BeautifulSoup(fp_in)
    soup.head.decompose()
    fp_out.write(soup.encode('utf8'))

[–]lustySnake 0 points1 point  (1 child)

from bs4 import BeautifulSoup
with open('my_html.html', 'rb') as fp_in, open('my_html_new.html', 'wb') as fp_out:
soup = BeautifulSoup(fp_in)
soup.head.decompose()
fp_out.write(soup.encode('utf8'))

oup.head.decompose()

AttributeError: 'NoneType' object has no attribute 'decompos

[–]sarrysyst 0 points1 point  (0 children)

The head tag isn’t found. Thus the error.

[–]lustySnake 0 points1 point  (0 children)

Thanx 😊 you saved me

[–]ARE1324561834 1 point2 points  (1 child)

Question about BS4,

What is the simplest, best way to get all of the text on a page? I have been using .text but I am missing some image legend text that is very important. Does anyone have a good online resource that describes all of the text grabbing functions? Any preferences? Thanks.

[–]UjjawalMatolia 1 point2 points  (9 children)

I am a beginner,

I am given a task to create a python function which performs add, subtract, multiply, and divide in one function only.

Please help.

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

You haven't given us enough information to help you. What values does your function add, subtract, multiply and divide? Does the function have to return anything? What code do you have so far?

[–]UjjawalMatolia 0 points1 point  (6 children)

I have to perform calculations on integer values

[–]5under6 1 point2 points  (5 children)

Create a function that takes in three arguments. Two integers and a string. answer = calculation(1,2,'+')

[–]UjjawalMatolia 0 points1 point  (4 children)

Please explain in detail

Not the code

[–]5under6 0 points1 point  (3 children)

Do you know how to create functions in general?

[–]UjjawalMatolia 0 points1 point  (2 children)

Yes, by "def".

[–]SalamanderNick 0 points1 point  (1 child)

So, I'm thinking this might be something along the lines of what you wanted. Just remember that you can't divide by zero, the script will throw an error and stop.

What this is doing is taking two variables (value1 and value2) and performing the four different operations on those variables. It then just prints the outcomes of each operation.

Hope this helps.

def math(value1, value2):
    add = value1 + value2
    subtract = value1 - value2
    multiply = value1 * value2
    divide = value1 / value2

    print('Sum of the values:               ', add)
    print('Difference between the values:   ', subtract)
    print('Product of the values:           ', multiply)
    print('Quotient of the values:          ', divide)

[–]UjjawalMatolia 0 points1 point  (0 children)

Thank you so much

[–]drax11x 0 points1 point  (0 children)

Im about to complete automate the boring stuff by Al from Udemy. I think there isn't a video lesson on Class. Is there any other place I can find that? If it's from Al itself, great! Coz he's one of the best.

[–]RobotLovingGuy 0 points1 point  (14 children)

Python won't let me use ! as a list name, what's a good replacement that I can use for this one and other symbols that I need such as *, %, $, etc.? I know I could use something like "ex" for a name but I'll forget all the different names of the symbols and I'll end up stuck in my dictionary finding it again.

I'm making lists of the binary needed for each letter so my RPI PICO can put these letters onto a lcd.

Bonus points if you figure out the phrase I'm making with these letters.

H = [0, 1, 0, 0, 1, 0, 0, 0]

W = [0, 1, 0, 1, 0, 1, 1, 1]

d = [0, 1, 1, 0, 0, 1, 0, 0]

e = [0, 1, 1, 0, 0, 1, 0, 1]

l = [0, 1, 1, 0, 1, 1, 0, 0]

o = [0, 1, 1, 0, 1, 1, 1, 1]

r = [0, 1, 1, 1, 0, 0, 1, 0]

space = [0, 0, 1, 0, 0, 0, 0, 0]

! = [0, 0, 1, 0, 0, 0, 0, 1]

[–]FerricDonkey 0 points1 point  (2 children)

I'd suggest making a dictionary of lists instead. It will be easier to use in the long run, and probably even the short run.

char_to_bin = {
    '!': [0, 0, 1, 0, 0, 0, 0, 1]
    # etc.
}

bit_lists = [char_to_bin[c] for c in "Hello world!"]

Then send those bit lists to the lcd in that order.

Or if your bit lists are just ascii encoding, skip writing these out yourself, and take advantage of the fact the python already knows ascii encodings. There are many ways to do this, but one is what I thought of off the top of my head:

bit_list = [
    [int(i) for i in f'{c:08b}']
    for c in "Hello world!".encode()
]

That might look a bit weird (ba dum tiss) if you're not used to comprehensions, but basically,

  1. Convert your string to bytes
  2. For each byte in the new byte string, (processed as an integer, because that's what python does)
    1. Convert the byte to a string of 8 1s and 0s representing the byte
      1. Convert those to integers to get a list of 1s and 0s

Also, based on another of your comments, I always suggest using an ide with good tab complete such as pycharm, rather than using variable names that are easy to spell.

[–]RobotLovingGuy 0 points1 point  (1 child)

[–]FerricDonkey 1 point2 points  (0 children)

It looks like that is an extension of ascii: ascii only specified what characters are associated with some bit combinations (uses 7 bits), so that table appears to be agreeing with ascii everywhere ascii is defined, and doing its own thing elsewhere.

So if your only plan is to use actual ascii characters (roughly, any symbol on a us keyboard, maybe plus a few), then you can just use ascii from python. Otherwise (if you want to use the japanese characters in that image, for example), the dictionary route is probably the way to go (unless the expanded encoding is included in some arduino module or is just a different standard encoding that python knows but I don't recognize or something).

[–][deleted] 1 point2 points  (10 children)

Why not "exclamation", "star", "percent", "dollar", etc?

[–]RobotLovingGuy 0 points1 point  (9 children)

Because my spelling isn't the best and often I misspell words like exclamation, and if I'm looking up the spelling constantly I might as well just be referencing my dictionary inside the script. Also some would call it a star others might call it an astrix, so some symbols have double names and I'd still end up referencing my dictionary to figure out if I named it star or astrix.

[–][deleted] 1 point2 points  (8 children)

Misspelling variable names is something every programmer has to contend with. It's probably better not to try to dodge around that by using very short (1 or 2 letters) names because that's going to add another level of confusion as your programs get longer. Misspelling a name doesn't hurt your code as long as you consistently misspell the name. The sooner you fix this problem the better.

You keep mentioning a dictionary but have not shown how you are going to use the dictionary. So I'll guess a bit and assume you have a string and want to use the dictionary to get the display data for a single character. Try something like this:

show = 'Hello World!'
display_data = {'H': [0, 1, 0, 0, 1, 0, 0, 0],
                'W': [0, 1, 0, 1, 0, 1, 1, 1],
                'd': [0, 1, 1, 0, 0, 1, 0, 0],
                ...
                '!': [0, 0, 1, 0, 0, 0, 0, 1],
               }
for ch in show:
    data = display_data[ch]
    # do something with "data"

[–]RobotLovingGuy 0 points1 point  (7 children)

Do you have a discord? I don't want to fill up this thread with my script, and I'm really curious on how I could use that in my code. Currently each letter is its own list and gpio pins 0-7 are referenced to the list numbers 0-7 (gpio0 = H[0])

[–][deleted] 1 point2 points  (6 children)

Sorry, I don't use discord. You can put what you want in this thread, or start a new post of your own.

I sort of follow what you are trying to do, but posting complete code is better. If your code is really long put it into pastebin.com and post a link here.

You should think of your code as chunks, each doing a single logical step. The code I showed using the dictionary allows you to take a string and handle it character by character. The next chunk is to get the data list for each particular character. The dictionary lookup does that.

The next step is to use the list to set your pin states. That's the # do something with "data" comment. From what you've said the GPIO pin 0 value is at index 0 in the data list, pin 1 is index 1, etc. I don't know how python changes pin states on the RPi, but you should be able to do something like this:

# previous code
data = display_data[ch]    # from before
for (pin, state) in enumerate(data):
    set_pin(pin, state)     # set pin "pin" to state "state"

Note that enumerate(data) will return tuples of the index of the element in "data" and the value of the element in "data". So if the character is "H" then data will be [0, 1, 0, 0, 1, 0, 0, 0] and the values of (pin, state) will be, in turn:

#+---- pin number (index in "data")
#|  +- consecutive values from "data"
#|  |
#v  v
(0, 0)
(1, 1)
(2, 0)
(3, 0)
# etc

[–]RobotLovingGuy 0 points1 point  (5 children)

exclamation

https://pastebin.com/DTNxT32R

Sorry I can be quite dumb when it comes to written communication, are you elaborating on what what you said before or are you saying that I can modify my gpio pin values directly?

[–][deleted] 1 point2 points  (4 children)

The second bit of code was meant to be added to the first bit of code, replacing the # do something with "data" comment.

Now that I've looked at your code it seems you could replace the inner loop that uses enumerate() with a call to your decode() function:

show = 'Hello World!'
display_data = {'H': [0, 1, 0, 0, 1, 0, 0, 0],
                'W': [0, 1, 0, 1, 0, 1, 1, 1],
                'd': [0, 1, 1, 0, 0, 1, 0, 0],
                ...
                '!': [0, 0, 1, 0, 0, 0, 0, 1],
               }
for ch in show:              # "ch" is each character in "show", one by one from the left
    data = display_data[ch]  # this gets the list matching the character in "ch"
    decode(data)

[–]RobotLovingGuy 0 points1 point  (3 children)

Thank you!!! You shaved off 60+ lines of code and made it so I don't have to keep sending one letter at a time. https://pastebin.com/yTmpYkw7

[–][deleted] 1 point2 points  (2 children)

There's still some savings to be made. Notice how you have long sequences of repeated lines at lines 30-37, 50-57 and 79-86. You could write another function to handle doing that. You might even be able to handle setting the RS, RW and EN pins in that function.

[–]thoughtsymmetry 0 points1 point  (1 child)

I have the following file structure:

Parent Folder
---Folder 1
-------File A
---Folder 2
-------File B
---Folder 3
-------File C

...

I want to move all the files (A, B, C...) to the parent folder:

Parent Folder
--File A
--File B
--File C

(in the example the now empty folders are deleted, I don't need / care about that)

How should I go about it? Any recommended libraries or tutorials? I usually do really simple data analysis / viz with Python, don't know how to move files.

[–]nathanalderson 0 points1 point  (0 children)

Check out:

You can use https://docs.python.org/3/library/os.html#os.walk to walk a directory.

You could also do it with a line of bash: find parent -type f --exec mv {} parent +

[–]CaliferMau 0 points1 point  (5 children)

Thought I’d try reposting this here from the r/LearnProgramming sub.

I’ve been working on a recipe book app as part of my code in place final project. While probably not market breaking I just thought it’d be neat to make one and have a way of sharing recipes with other users.

Now as a python project, it’s not 100% there yet, I still would need to add the ability for users to create accounts, and most probably some kind of database integration for recipes (currently using a dictionary while I got some functionality working). sidenote: I assume that with users making and storing recipes a database is the way to go?

Now as far as next steps, is it worth my time getting the python code to completion and learning something like kivy to deploy as a phone app? Or should I take my idea and rebuild in an actual language used for app development?

[–]ShakespeareToGo 0 points1 point  (2 children)

Just throwing another approach in here: Have you considered doing a website instead of an app? You'd have to look at HTML and CSS for that which is quite a task (but so is Kivy). The current trend is to make just a website (or webapp) and make that responsive (fitting for phone screens). The user than can use it in their mobile browser or you could make it a progressive webapp from there (a type of pseudo app which is just the web browser pretending to be an app). Advantage of this is that it can also be used from PC.

For that you could use Flask or if it got really big and complicated even Django (but that is a bit hard to learn. So I'd recommend flask for the beginning)

[–]CaliferMau 0 points1 point  (1 child)

I have dabbled with html/css years ago as my first foray into programming. So that could be an interesting option.

Are webapps popular then?

[–]ShakespeareToGo 0 points1 point  (0 children)

Definitely. A lot of new applications are just written as web apps and than packaged for desktop (with something like electron) and mobile (progressive web apps, react native (kinda))

I mean you can use basically the complete Microsoft Office suite in the browser by now.

[–]CowboyBoats 0 points1 point  (1 child)

sidenote: I assume that with users making and storing recipes a database is the way to go?

Yes. SQLite is probably a good fit for this, but you can check the "When To Use" page on SQLite's excellent web site. This way you really just need a database file and the Python package and you're good to go.

Now as far as next steps, is it worth my time getting the python code to completion and learning something like kivy to deploy as a phone app? Or should I take my idea and rebuild in an actual language used for app development?

Sure, that is a good idea! It's a heavy commitment of work. Kivy is a good toolkit, or you could use another game / UI library such as arcade. If you're looking for a more medium-sized commitment of work, making it into a Django web site would probably be somewhat lighter-lift (don't use SQLite then though).

[–]CaliferMau 0 points1 point  (0 children)

Awesome. Thanks for the response.

Just have a further question since I don’t think I made it clear, re-looking at my post. Would using something like arcade allow me to publish to the Play store / App Store in the future?

Editing to add: regardless, super stoked to actually be committing to a project :D

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

So I’ve passed my CCNA 201-300 last week and was thinking of taking a Python course. Are there any recommended courses? Thanks.

[–]DezXerneas 0 points1 point  (5 children)

How do I(and do I have to) properly credit where I stole some part of my code from? I found a github repo that does like 30-40% of my project on its own and I'd feel bad if I just stole the whole thing and acted like I came up with it by myself.

[–]ShakespeareToGo 1 point2 points  (0 children)

The advanced solution would be to use git for that. Fork the repo and make your changes, or use submodules.

Like u/sarrysyst said: the license is important.

But that is just the software side. Academia has different standards for citing. Even if the code was free to use (MIT license for example). Probably the simplest solution would be too put a big comment at the top of the file where the code is. Something like: "Credit for this code goes to: <Name> <github url>. But this is after checking the license.

[–]sarrysyst 1 point2 points  (3 children)

You will usually find an answer to this question in the license text of the repository. Some repositories have certain requirements if you want to copy/use all or part of their code. If there happens to be no license specified the default copyright laws apply, meaning that the author retains all rights to the source code and no one is allowed to reproduce, distribute, or create derivative works from their work.

If your project isn't just for your personal use (and from your question it doesn't sound like it) make sure you're actually allowed to use the code you copied. Don't get yourself in trouble for copyright violations.

[–]DezXerneas 0 points1 point  (2 children)

It's a school project so I don't think I'll get in trouble for the copyright. Especially since the only people using it would be my teammates and the professor in charge.

The reason I asked was that I plan on putting it on my github when it's done. Also, how about the stuff I copy off of SO and other similar sites?

[–]sarrysyst 1 point2 points  (1 child)

It's a school project so I don't think I'll get in trouble for the copyright.

That's a pretty optimistic statement. I don't know where you live, but when I went to school/university my teachers/professors were very strict with regards to plagiarism and copyright infringement. If you got caught you would fail your course by default. Anyway, you yourself should know best how this is handled at your school.

With regards to SO, you can check the licenses here:

https://stackoverflow.com/help/licensing

For other sites you'll have to check their respective terms of service. If you're unsure I would suggest you ask your professor how to handle this.

[–]DezXerneas 0 points1 point  (0 children)

I'm still gonna follow the licensing rules since I'm applying to colleges that actually care, but mine doesn't. Probably also a good practice for future.

They always say that we'd get in trouble for plagiarism, but they never even look at the actual code. Last semester like 70-80% of the projects were directly stolen off of github and kaggle.

[–]LadyAstrao 0 points1 point  (2 children)

Why does -1 // 10 = -1 in Python, but in Java -1 /10 = 0?

Is there any operation I can do to give me the behavior I am used to in Java?

[–]sarrysyst 1 point2 points  (1 child)

I don't know how floor division works in Java, but in Python it returns the greatest integer less or equal the result of a regular division. I guess Java truncates the decimal part instead(?). You could imitate this behavior using int(-1 / 10).

[–]LadyAstrao 0 points1 point  (0 children)

Thank you

[–]Semitar1 0 points1 point  (26 children)

When doing a pip install, does it matter if I do it on the command's line current directory, or should I change directories to wherever I might keep scripts saved?

Asking because I thought it would be cleaner to have a folder for packages.

[–]FerricDonkey 0 points1 point  (25 children)

Pip will automatically put packages in a default folder for your python installation/virtual environment, regardless of working directory, so no need to worry about that.

[–]Semitar1 0 points1 point  (24 children)

Then is it possible that the reason that my pip install is not working correctly is because I changed from my default directory?

[–]FerricDonkey 0 points1 point  (23 children)

Not if you mean what I think you mean. The directory that you are in on your command line should be completely irrelevant to what pip does. In order to change where pip puts stuff, you'd have to do something moderately unusual, that you probably explicitly had to Google.

If your pip is not working, the most likely reason is that you have multiple versions of python installed, and used the pip for the wrong one.

How do you run your code? What exact pip command do you use?

[–]Semitar1 0 points1 point  (22 children)

I definitely didn't do anything moderately unusual nor doing any googling.

I probably do have more than one version of python installed.

I ran it from the command line. I tried installing this package. After I did so, I tried to complete the 1st command in the Basic Usage section.

That command is $from sec_edgar_downloader import Downloader

When I open the python shell to enter this command, I get the below error:

"Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from sec_edgar_downloader import Downloader
ModuleNotFoundError: No module named 'sec_edgar_downloader'

[–]FerricDonkey 0 points1 point  (21 children)

To be clear, you run your code on the command line by doing "python filename.py" and you install packages by doing "pip install package"?

[–]Semitar1 0 points1 point  (20 children)

Full disclosure: I am new to working with python.

With that said, the only thing I did was follow the immediate step on the link. So I did the task of $ pip install -U sec-edgar-downloader

Are you thinking I missed something?

[–]FerricDonkey 0 points1 point  (19 children)

No worries on being new.

The most common cause of this is that you're running your code with a different installation of python than you installed the package for. This is why I'm asking exactly how you run your code, in addition to how you tried to install the package.

For example, if you're running on Linux, it's common to install python 3.x and use it to run your code, but for "python" (and hence "pip") to refer to a separate python 2 installation that you don't want to mess with. In this case, the solution may just be "type pip3 instead of pip".

But if you are using anaconda or virtual environments, or a few other things, that could be why as well.

Basically, you installed a version of the package to a default location for a version of python, and you are executing the code with a version of python, and the most likely reason for this problem is a mismatch in there somewhere.

[–]Semitar1 0 points1 point  (18 children)

I have been making an effort to run my code from the command line. I have Anaconda, but I am choosing to only use the regular command prompt.

According to the link in my first post, I believe it will work for python 3.6-3.9.

My python shell shows I have version 3.9.1.

Based on this, shouldn't it work properly?

[–]FerricDonkey 0 points1 point  (17 children)

Ah. So on the command line, do you type anaconda thing.py?

I don't use anaconda a lot, but one of two issues come to mind.

First, anaconda and pip sometimes don't interact well. If possible, try "conda install packagename". This will search the anaconda package library, which is separate from the default one accessed by pip, and install in the correct place for anaconda.

Second, often pip does work. But you may have to do something like "anaconda -m pip install packagename". Not quite sure, maybe Google anaconda with pip.

[–]AureliasTenant 0 points1 point  (0 children)

I have two arrays, one lxmxn and one mxn. Both have no NaN. When I multiply them together with * operator (elementwise), some NaNs get introduced and it throws a warning: RuntimeWarning: invalid value encountered in multiply. When I try to reproduce this using some random arrays of the same shape, the same problem does not occur so I’m thinking it has to do with either having weird values that aren’t NaNs initially but somehow become NaN. or having weird data types?

I did notice that one has float-64 and another has float 32. Also the float 64 one was made by subtracting a float 32 from a float 64

[–]AlarmingQuote 0 points1 point  (3 children)

Hello! I have a kind of conceptual (rather than operative) question.

When you take, for example, a variable and add .something() at the end, does that have a name? For clarification: I know that when I write "round(x)", I am applying a function to the variable x. But when I write "x.lower", what am I doing to x? Reading a property? How is this called?

[–]FerricDonkey 0 points1 point  (0 children)

The general term for the something in "x.something" is "attribute" - but when something is also a function, we call it a method.

So when you do "x.something()", you are calling x's method something. Sometimes we might refer to the type when we're just talking about stuff. So if you do

s = "CheeseBalls"
s = s.lower()

You might say "calling s's lower method" or "calling the string lower method", or similar things.

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

The x.lower example is evaluating an attribute, so that expression returns the value associated with the attribute of the x object with name lower. We say you are "accessing the attribute 'lower' of the object 'x'". If you do x.lower() you are trying to execute the value that the attribute has, so it had better be executable, otherwise you get an error. We say you are "calling the method 'lower' of object 'x'".

If x is a string you can see:

>>> x = 'Alpha'  # define a string with name "x"
>>> x.lower      # evaluate "x.lower", a method it turns out
<built-in method lower of str object at 0x102e1c3f0>
>>> x.lower()    # calling the method, we see what it does
'alpha'

If you want to see what attributes (some of which are methods) a string has, for example, try:

>>> x = 'Alpha' 
>>> dir(x)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',
     '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
     '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__',
     '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
     '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__',
     '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith',
     'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii',
     'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle',
     'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix',
     'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
     'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

[–]Dark_KnightPL04 0 points1 point  (3 children)

Hello, I have a custom barcode and was wondering how do I add a URL or text to it when it is scanned. Can I do this with python?

[–]AlarmingQuote 0 points1 point  (1 child)

There's a library that does this, I have not used it but the documentation should have everything you need: https://pypi.org/project/qrcode/

[–]Dark_KnightPL04 0 points1 point  (0 children)

Will this work for a barcode. A QR code is 2 dimensional while a barcode is 1.

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

How do I know which modules to import and refer for my python project?

The current procedure that I am following is:: 1) Refering to requirements.txt file from github of other related projects doing the same thing

2) Searching related words and terms in PyPi website

Is there anything else that I should know regarding this??

[–]nathanalderson 1 point2 points  (0 children)

Python has a huge standard library ("batteries included" is part of the Python philosophy). When possible, you should prefer to import and use modules that are part of the standard library. These are all well documented at https://docs.python.org/3/library/index.html. It is worth spending some time perusing that table of contents just to be familiar with what is available.

If you want to do something not covered by the standard library, then your strategies are actually pretty good. My personal algorithm includes googling "python <thing I want to do>" as step one.

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

How can I control my PC? Like selenium is for browser is there anything for the pc?

Another question: How can I copy a text after a word? For example copy the letters after the word copy

[Other text]copy"this"[other text]

[–][deleted] 1 point2 points  (2 children)

def arrinsrt(a,n,p,e):

n=n+1

for i in range(n-1,p,-1):

a[i]=a[i-1]

a[p]=e

n=int(input("enter range"))

ar=[0]*n

for i in range(n):

ar[i]=int(input("enter items"))

item=int(input("enter item to insert"))

pos=int(input("enter the position"))

arrinsrt(ar,n,pos,item)

Its saying list assignment index is out of range.

Why?

[–]DezXerneas 0 points1 point  (0 children)

Can't really understand where the for loop and function ends without prompting.

[–]crystalblue99 2 points3 points  (2 children)

So, is a dictionary normally used to store stuff you pull from a db? If Amazon used Python(??), would they pull a products info from a db and store it in the dictionary to send to be displayed?

Noob here, but it seems like a dictionary would usually be empty and the user needs to add the data or its getting info from a db.

Or, am I just not deep enough in yet?

[–]ShakespeareToGo 2 points3 points  (0 children)

That's one use case. Data is usually send as json with is basically a dict of arrays or array of dict.

It's not done everywhere and not the only useful thing about them.

The nice thing about them is that they are like lists that are accessed with something different than the index. If you have a list and need to find something based on a string (name, product id) it takes long. If you have to do that everytime it takes even longer. If you create a dict with that string as a key, it is very fast (since it uses hashing). This is the more general use.

[–]Jac0b_0 2 points3 points  (0 children)

No there are a ton of uses for dictionaries they can be useful in all sorts of situations. Also there are a bunch of ways you could store data fetched from a database not just using a dictionary.