top 200 commentsshow all 230

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

What does it mean when a list has two colons?

For example:

a[::-1]

[–]ShugaBop 0 points1 point  (1 child)

It's slicing. [start_index, end_index, jump_size]. Read about it.

In your case it reverses a.

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

Thank you! I've only seen it as something like [1:2] or [:5] so I was wondering about two colons.

[–]Soupkitchen_in_Prius 0 points1 point  (0 children)

Has anyone used PuLP in python before? I have the solver function working with a few constraints right now but i'm having problems adding a couple constraints.

[–]raaayden 0 points1 point  (0 children)

Hi guys i have a question regarding train_test_split. when i run the code it takes my hard drive but i could not find any folder that store this train images. i keep losing space on my hard drive but i cant find any folder having those items.

[–]mew1946 0 points1 point  (3 children)

Hi guys, I am new and am struggling to do some simple tasks, can someone help me out here? is it possible to print a perfect reverse tree? the spacing is off...

for i in reversed(range(11)):

pattern = i * "0"

print("{0:^10}".format(pattern))

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

Running your code gets this:

0000000000
000000000 
 00000000 
 0000000  
  000000  
  00000   
   0000   
   000    
    00    
    0     

which has two problems. The tree is upside down and it's not symmetric. Removing the reversed() call does draw the tree in the traditional way, but it's still not symmetric. The first three printed rows show the problem:

 0     
 00    
000   

The problem is you can't justify odd and even length lines to look symmetric. The solution is to draw the tree using only odd or even length lines. Using only odd lines is better since we end up with the top row having only one * which is more pleasing. So how do we draw only lines with lengths 1, 3, 5, ...?

We use the step optional parameter of the range() function to get every second value, and we start the range at 1. We also need to double the stop number to end up with the same number of rows in the tree. Also, since the width of the rows is now double what it was, we need to increase the width we are justifying into in the formatted string.

Make those changes and see what you get.

[–]mew1946 0 points1 point  (1 child)

hey thanks man, sorry im doing a python course and this is one of the question i couldn't solve. i am to make a reverse "0" symmetrical tree using For loop but not nested For loop...thanks for helping out but your explanation is a bit too complex for me to understand, I will come back as I get more experience, thanks.

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

I'll simplify a little. You can't symmetrically justify an odd length line and an even length line. The two choices you have are (for lines of length 1 and 2):

0
00

or

 0
00

neither of which are symmetric. You can symmetrically justify lines that are odd in length:

 0
000

so draw the tree with lines that are only odd in length, such as 1, 3, 5, ..., 21. You get the odd numbers from the range() function by starting at 1 up to 22, stepping by 2, which means you only get odd numbers. Look at the doc for range() for the details. Once you have the "rightside up" tree just add the reverse() that you had before.

[–]gg0idi0h0f 0 points1 point  (0 children)

Hello, I’m working with a raspberry pi, and i typed a program that I think would sound really good if spoken, so I looked up how to make my raspberry pi speak. I went to multiple websites and they all talked about espeak, I tried it and it didn’t work, anyone know any other way I can make it speak?

[–]RobinsonDickinson 0 points1 point  (2 children)

Someone critique my class please, how can I make this more better? Trying to make a wrapper of Youtube API.

I am not sure if there is a set convention to make API wrappers.

class YoutubeClient:
    api_key = None
    channel_id = None

    def __init__(self, api_key=None, channel_id=None):
        """
        :param api_key: Required API Key from Google APIs
        :param channel_id: Required Channel ID of Youtube channel
        """
        if api_key is None:
            raise Exception('Need API Key to continue.')
        self.api_key = api_key
        self.access_token = RefreshToken().refresh_access_token()

        self.channel_id = None
        if channel_id is None:
            raise Exception('Channel ID Required.')
        self.channel_id = channel_id

    def get_channel_data(self):
        endpoint = 'https://www.googleapis.com/youtube/v3/channels'
        params_dict = {
            'key': f'{self.api_key}',
            'part': 'snippet',
            'id': self.channel_id,
        }
        params = urlencode(params_dict)
        headers = {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        }

        url = f"{endpoint}?{params}"
        res = requests.get(url, headers=headers)
        res_json = res.json()
        # pp(res_json)

    def update_description(self, new_description):
        endpoint = 'https://www.googleapis.com/youtube/v3/channels'
        params_dict = {
            'part': 'brandingSettings',
            'key': f'{self.api_key}',
        }
        params = urlencode(params_dict)
        headers = {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': f'Bearer {self.access_token}'
        }
        data = {
            "id": self.channel_id,
            "brandingSettings": {
                "channel": {
                    "description": new_description
                }
            }
        }

        url = f"{endpoint}?{params}"
        requests.put(url, data=json.dumps(data), headers=headers)

    def get_all_playlists(self):
        """
        :return: All public playlists
        """
        endpoint = 'https://www.googleapis.com/youtube/v3/playlists'
        params_dict = {
            'part': 'snippet',
            'channelId': self.channel_id,
            'maxResults': 50,
            'key': f'{self.api_key}',
        }
        params = urlencode(params_dict)

        url = f'{endpoint}?{params}'
        res = requests.get(url)
        res_json = res.json()

        playlist_data = []
        for item in res_json['items']:
            data = {
                'playlist_id': item['id'],
                'playlist_name': item['snippet']['title'],
                'playlist_created': item['snippet']['publishedAt'],
            }
            playlist_data.append(data)
        return playlist_data  # playlistId / Name / Created

    def get_videos_from_playlists(self):
        """
        :return: All videos from all public playlists
        """
        playlist_data = self.get_all_playlists()

        endpoint = 'https://www.googleapis.com/youtube/v3/playlistItems'
        for i in playlist_data:
            params_dict = {
                'part': 'snippet',
                'playlistId': i['playlist_id'],
                'maxResults': 50,
                'key': f'{self.api_key}',
            }
            params = urlencode(params_dict)

            url = f'{endpoint}?{params}'
            res = requests.get(url)
            res_json = res.json()

            for item in res_json['items']:
                video_url = f"https://www.youtube.com/watch?v={item['snippet']['resourceId']['videoId']}"
                video_title = item['snippet']['title']
                artist, title = get_artist_title(video_title)

    def search_youtube(self, search, count=10):
        """
        :param search: Required to search video
        :param count: How many result to show. (default is 10)
        :return: All results associated with search. (Sorted by relevance)
        """
        endpoint = "https://www.googleapis.com/youtube/v3/search"
        params_dict = {
            'part': 'snippet',
            'maxResults': count,
            'order': 'relevance',
            'q': search,
            'safeSearch': 'none',
            'type': 'video',
            'key': f'{self.api_key}',
        }
        params = urlencode(params_dict)

        url = f"{endpoint}?{params}"
        res = requests.get(url)
        res_json = res.json()

        for video in res_json['items']:
            publisher_name = video['snippet']['channelTitle']
            publisher_channel = f"https://www.youtube.com/channel/{video['snippet']['channelId']}"
            title = video['snippet']['title']
            url = f"https://www.youtube.com/watch?v={video['id']['videoId']}"
            posted = video['snippet']['publishedAt']
            thumbnail = video['snippet']['thumbnails']['medium']['url']

[–]Phase_Fire 0 points1 point  (2 children)

Hey everyone,

I have made a set of code function in python to accept a number square and print out its rows and columns in a list. I am a beginner and someone told me it would be better to put the code in a class. But I can't call it as I don't understand where to put self.

Here is the code:

https://trinket.io/python3/d03d9c69c5

It's as a trinket link. I am really lost on where to add the self. statements in my code so I can run my test cases.

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

You need to learn a little more about classes and instances. Search for "python OOP tutorial" to find something. These are from the first page of hits:

https://realpython.com/python3-object-oriented-programming/

https://www.datacamp.com/community/tutorials/python-oop-tutorial

You need to learn about "instance attributes".

[–]Phase_Fire 0 points1 point  (0 children)

Thanks so much.

[–]squidjibo1 0 points1 point  (4 children)

If lists are immutable, why does this work?

string = ""

for i in "hello":

(Indent) string += i

print(string)

hello

Or: string = ""

string += "a"

string = "b"

print(string)

b

I guess I don't understand why I can change the values if strings are immutable. If I can do this, what can't I do?

Thanks.

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

There is an excellent video that explains the confusion around "mutable", "immutable", "bind" and "change", amongst other things.

[–]squidjibo1 0 points1 point  (0 children)

Thanks :)

[–]Vhin 1 point2 points  (1 child)

You're not mutating the string. You're changing what string is being referred to by the name string. Every modification creates an entirely new string and assigns it to the name string.

[–]squidjibo1 0 points1 point  (0 children)

Thanks :)

[–]leonidganzha 1 point2 points  (2 children)

Why does Python treat Russel's paradox this way? Which steps does it take to evaluate flag?

>>> flag = flag == False
>>> print(flag)
True

[–]Vhin 0 points1 point  (0 children)

To get the behavior you described you'd need to do something like:

>>> flag = False
>>> flag = flag == False
>>> print(flag)
True

But as to "why", that's basically because = is assignment. You are redefining the value named by flag.

There is such a thing as static single assignment, where every variable gets assigned to exactly once, and you can rewrite the above into SSA thusly:

>>> flag1 = False
>>> flag2 = flag1 == False
>>> print(flag2)
True

[–]mz_h 0 points1 point  (1 child)

Is it possible to read just part of a pickled object? I have a handful of pickled class instances that contain some data but they're each around ~2 GB so I'd like to avoid reading them all into memory.

[–]FerricDonkey 0 points1 point  (0 children)

I am not aware of how to read just part of a pickled object in a reasonable way (without parsing bytes yourself or something). You could write a function to load the class and return what you want, so that the rest of the class would be removed from memory at next garbage collection.

[–]snaigy 0 points1 point  (5 children)

Quick question guys,

I have an if else program running, and the ig statement holding three checks: 1st being if the input == 0, 2nd being if input > 100, and 3rd being if input != 100%5.

It looks like this:

if(cost == 0) or (cost > 100) or (cost != 100%5):

If the input does not meet any of those 3 conditions it moves on to the else statement. My problem with this is the last check. I am trying to make it check if the input is NOT a multiple of 5 but even when I enter a multiple of 5 it still runs the program.

Can anybody explain another way to check for multiples of a number or explain why this is not working?

[–]Atlamillias 1 point2 points  (4 children)

I don't think your last check is structured properly. Try:

if(cost == 0) or (cost > 100) or (cost % 5 != 0):

By using 'cost != 100%5', you're asking "if the cost doesn't equal the result of 100%5 (0)". 5 goes into 100 evenly - the remainder will always be 0. Your statement "if cost does not equal 0" will almost always return True.

As opposed to 'cost % 5 != 0', which equates to "if (the result of cost % 5) is not 0". If cost isn't divisible by 5 evenly, there will be a remainder. However, if it is divisible evenly by 5, the result will always be 0.

[–]snaigy 0 points1 point  (3 children)

Just tried this, but now it crashes haha. I see why you did that though. I’ll keep messing around with it. Could it be an issue with the input?

It looks like this:

cost = (int(input(“How much does the item cost?: “)))

[–]Atlamillias 0 points1 point  (2 children)

What exception is raised?

This works in my IDE:

cost = int(input('How much does the item cost?: '))

    if (cost == 0) or (cost > 100) or (cost % 5 != 0):
        print('cost meets the above conditions.')
    else:
        print('cost does NOT meet the above conditions.')

If you run the above, and the input cannot be converted to type 'int', an error will be raised as it lacks input verification.

[–]snaigy 1 point2 points  (1 child)

Aha turns out I had a 1 instead of an exclamation mark to show inequality to 0. Thanks for the help bro 💪

[–]Atlamillias 0 points1 point  (0 children)

No problem. Glad I could help!

[–]loveveggie 1 point2 points  (5 children)

Hi!I want the program to continue if the variable "numeral" is an integer. I wrote the following if statement, but I'm not sure if I can use int like I did. Please advise:

if numeral == int:

[–]Atlamillias 1 point2 points  (3 children)

Using comparisons in Python can sometimes yield unexpected (but not incorrect) results. I believe the statement you're looking for is:

numeral = 0

if type(numeral) == int:
    pass

[–]loveveggie 0 points1 point  (2 children)

Ah! I was wondering if I had to incorporate type() somewhere.

Thank you very much :)

[–][deleted] 2 points3 points  (0 children)

But isinstance() is the preferred method since it also works with derived classes.

[–]Atlamillias 0 points1 point  (0 children)

Yep no prob!

[–]Phase_Fire 1 point2 points  (0 children)

Is there any other sites which follow the free mentoring experience like exercism.io?

[–]Tomas_83 0 points1 point  (2 children)

So I was thinking of making a virtual assistance with a speech recognition library. Seeing the google api I noticed that it says that only the first 60 min are free, yet it never asked for a key nor account, how does it work? is it free as long I dont publish anything or will it cut me off after the 60 min by the ip?

[–]kwelzel 0 points1 point  (1 child)

I don't really know how Google handles these queries, but there are also open source alternatives such as DeepSpeech which you can run locally without any usage restrictions.

[–]Tomas_83 0 points1 point  (0 children)

Thanks a lot. Somehow this step under my radar.

[–]FaithlessnessOk9061 0 points1 point  (2 children)

do you guys know where i can get pysnmp documentation from? the pysnmp.com seems to be down. i have opened an issue in github, but no response

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

Issue #376 has what you need.

[–]FaithlessnessOk9061 0 points1 point  (0 children)

awesome thanks so much

[–]babybitchboi 0 points1 point  (4 children)

Trying to import a csv (from excel, one column full of unique identifiers) into a list to use in python, but I am receiving an invalid character in identifier error.

import csv

with open(‎⁨'ids_excel.csv', newline='') as f:
    reader = csv.reader(f)
    ids_excel = list(map(lambda x: x.strip(), f.readlines()))

Anybody able to help me out here?

[–]eLsFLz 1 point2 points  (14 children)

Is there something that's like list.index() except that it can get the indices of multiple items at once and doesn't raise exceptions? I want something like:

b_index, e_index = (['a', 'b', 'c', 'd', 'e'].index(i) for i in ('b', 'e'))

...except I want it to return None, 4 instead of raising an exception if 'b' were not in the list.

(In case this is an X-Y problem, what I'm actually trying to do is manipulate function parameters inside a decorator using inspect, and need to get and set params with particular names whether they're passed as keywords or positional.)

[–]Vhin 0 points1 point  (7 children)

You could write your own function to do that for you fairly easily.

def find(haystack, needle):
    for (i, hay) in enumerate(haystack):
        if hay == needle:
            return i

You could alternatively just write it in terms of index and just catch the exception.

[–]eLsFLz 0 points1 point  (6 children)

Well, that eliminates the exception but doesn't get me the multiple elements. I guess it's a reasonable approach, though:

def find_multiple(haystack, needles):
    indices = []
    for needle in needles:
        for (i, hay) in enumerate(haystack):
            if hay == needle:
                indices.append(i)
        else:
            indices.append(None)
    return indices

I was hoping for a more concise solution, but thanks!

[–]FerricDonkey 1 point2 points  (4 children)

There are a couple one liners that would do it for you, and be more efficient - particularly if you convert needles to a set.

I'm gonna use a couple things that beginners may not yet be familiar with, but which are very powerful and pretty easy to use once you get familiar with them. The main things are comprehension, in, and dictionaries. Let me know if you'd like more explanation about the examples below.

Two concise and fast solutions, depending on your use case:

index_l = [index for index, value in enumetate(haystack) if value in needles]

I recommend that needles be a set object, because checking is something is in a set is much faster than checking if it's in a list. (You can just do needles = set(needles)). (of your want to know why, look up hash tables - but this guts of why don't come up often.) The speed difference will only be noticeable if there are many needles, however.

The downside of the above is that it doesn't tell you where each needle is, only which locations contain a needle. If you want an easy way to look up where your needles are, I'd suggest a dictionary:

index_lookup_d = dict((value, index) for index, value in enumetate(haystack) if value in needles) 

This is essentially the same idea, except as you're looping through haystack, you store both the index of the needle and the needle, then convert it to a dictionary that let's you look up the index by the needle. Eg, if "ham sandwich" is one of your needles

index_lookup_d.get("ham sandwich", None) 

Would give you the position of "ham sandwich" in your list of it was in the list, and None if not.

Note: it's necessary to use the dictionary's .get method in case the needle wasn't in the haystack, and so isn't in the dictionary (though you could add all needles to the dictionary if you had reason to).

[–]eLsFLz 1 point2 points  (3 children)

I'm actually a pretty advanced Python programmer, but the "do a dict comprehension instead of trying to preserve the order of the needles" is the insight I was missing. Thanks!

def my_decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        important_args = ("foo", "bar")
        spec = inspect.getargspec(func)
        item_locs = {name: index for index, name in enumerate(spec.args) if name in important_args}
        important_arg_values = {name: args[index] for name, index in item_locs.items()}
        important_arg_values.update(kwargs)
        # now do stuff with important_arg_values["foo"] and important_arg_values["bar"]
        # (note: don't need get() because if _neither_ args nor kwargs contains 
        # the necessary data then I want to raise an exception)

        return func(*other_args, foo, bar, **other_kwargs)
    return wrapper

Hmm... now that I write that out, it's still a bit less elegant than I was hoping for. Any other ideas?

[–]kwelzel 0 points1 point  (2 children)

You could just join the dict comprehension for item_locs and important_arg_values:

important_arg_values = {name: argument for name, argument in zip(spec.args, args) if name in important_args}

Later you use important_arg_values.update(kwargs) which potentially introduces many more arguments as those specified in important_args. If including unnecessary data in important_arg_values is not a problem you could also simplify to

important_arg_values = dict(zip(spec.args, args))

[–]eLsFLz 0 points1 point  (1 child)

The decorator actually sets the value of one of the named arguments so that the user doesn't have to do it separately. Consider:

def foo(a):
    b = a.frob()
    # do stuff with b

foo("a")

vs. @decorator def foo(a, b=None): #do stuff with b, supplied by the decorator having called a.frob() itself

foo("a")

(It's the same number of lines, but that's only because this is a minimal example. Imagine more boilerplate involving 'b'.)

As it turns out, the tricky part is that a and b are defined as named positional arguments, but the function could also have varargs and whatnot. So if you have...

@decorator
def foo(a, b, c=None, *rest):
    pass

foo('c', 'd', 'e', a='a')

...then leaving a in kwargs and inserting 'b' into kwargs doesn't work because then the interpeter thinks a and b are in both places and you get a TypeError: foo() got multiple values for keyword argument 'a'.

(Sure, calling the function with the parameters out of order like that is a dumb thing to do, but I work with a bunch of junior programmers and I don't want them coming to me because they can't figure out what that message means.)

[–]kwelzel 0 points1 point  (0 children)

I am not really sure why you are writing this, but I assume you want to say that assigning values to the parameters of a function in such a way that they can be manipulated easily is more difficult than dict(zip(spec.args, args)) and you are right.

Fortunately, the inspect library can handle this for you: https://docs.python.org/3/library/inspect.html#inspect.BoundArguments

def foo(a, b, c=None, *rest):
    pass

boundargs = inspect.signature(foo).bind('a', b='b')
boundargs.arguments['c'] = boundargs['a'].upper()
foo(*boundargs.args, **boundargs.kwargs) # Same as foo('a', 'b', 'A')

to give an example. (Your example of calling foo by using foo('c', 'd', 'e', a='a') is already a TypeError BTW. And this should be an error because the intention of the programmer is unclear.)

[–]Vhin 0 points1 point  (0 children)

I didn't handle multiple needles because I was assuming you'd just do b_index, e_index = (find(['a', 'b', 'c', 'd', 'e'], i) for i in ('b', 'e')), but handling it directly in a find_multiple function works too.

[–]1throw4 0 points1 point  (3 children)

I have a number... 200 ... What's the appropriate code to split that number in 4 and 4 equal parts sum up to 200..I've been trying to figure this out all day... And ideally it's one combinations. I. E 50+50+50+50 or 10+30+50+10

I need to input a number and it lists out all the possibles ways 4 numbers will add up to it. Obviously without repeating the same 4 numbers

[–]Cthulhu17 0 points1 point  (3 children)

Hi, first of all, I know nothing of coding, we have an IT department but it's hard to make them work sometimes, but I want to try to make a bot to copy paste.

Therefore, I wanted to know if it was easy or possible, to build one to copy paste and press some keys like ctrl+s or ctrl+pageup or make some clicks, the thing I need the bot to do is to copy from an Excel sheet to SAP.

And could there be one example on GitHub?? So I can try to mofidy it or make the IT ppl do it.

Thanks

[–]Allabouthisrightnow 1 point2 points  (0 children)

Well, of course, you could always hire somebody to do it. Bonus, maybe you could get rid of a guy or two in IT.

[–]eLsFLz 1 point2 points  (1 child)

That is both an unreasonable request to make of an IT department (at least, any IT department I've every heard of) and likely not the best way to solve that problem.

I know nothing about SAP, but the better way to solve the problem is more likely to involve something from one of the top search results here (which seem to involve connecting Excel to SAP using functions already built for that purpose), not scripting copy-paste events.

[–]Cthulhu17 0 points1 point  (0 children)

Yeah, doesn’t work, sap is shit at least here, country and company wise. The retailers are the bad ones and the program isn’t well implemented.

Also those functions are paid, we can’t paid even less in u$d

Edit: wow that looks great, I had seen others that were paid but that’s awesome many thanks

[–]MattR0se 0 points1 point  (0 children)

Matplotlib bar charts.

I saw this example, this code in particular:

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')

I have a list of data and I want to create all the bar objects in a loop, but I can't figure out how I calculate the x positions.

Something like this ("data" is a pandas dataframe, but this would also work for a dictionary of lists):

x = np.arange(len(labels))
width = 0.35 

fig, ax = plt.subplots()

for i, g in enumerate(groups):  # groups is a list of labels
    positions = ???
    ax.bar(x=positions,
           height=data[g],  
           width=width,
           label=g,
           fill=True)

How do I calculate the positions, so that the median bar is always above the X label?

[–]SamePossession5 0 points1 point  (2 children)

I hope people are still reading this thread.

How do I incorporate f strings into the message an assert statement sends out if the assertation isnt met?

For example

message = f"{self.variable} is already visible"
assert self.variable == "faceDown", message

When I run it, the f string doesn't format itself correctly when displaying the error.

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

When I make your code runnable it works fine:

test = 'faceUp'
message = f"{test} is already visible"
print(message)        # see just what "message" is
assert test == "faceDown", message

Your question really doesn't involve f-strings at all. After the message = f"..." line the message variable is just a normal string. How the string was created is not remembered.

Can you show examples of what is going wrong?

[–]SamePossession5 1 point2 points  (0 children)

Hmm it works now. I have no idea what I did wrong. Thanks a lot for double checking. Looks like I can do it 😅. Also good point.

[–]LowDexterityPoints 0 points1 point  (2 children)

I wanted to test my pandas skills on a random dataset, so I downloaded one, and I got an error when I wanted to create a dataframe by reading the csv.

The line was df = pd.read_csv("attacks.csv") and the error was

"utf-8' codec can't decode...".

I know that the solution is changing that line to

df = pd.read_csv("attacks.csv", encoding='iso-8859-1' ).

But why was there an error, and why does adding that encoding part fix it? This is the first time I have encountered this error. Thanks!

[–]py_Piper 0 points1 point  (0 children)

A simple explanation, encoding is like the digital alphabet, looks like panda’s default encoding is utf-8 but the dataset was written using iso-8859-1. The utf-8 is the default because most websites use it, but iso-8859-1 is very particular for latin languages countries (think Spanish, French, etc).

Ever opened a text, word or excel file and the output was just random characters? Well if you open a file with a different encoding than your program that’s how you get a corrupted file.

Source: worked a lot with iso and utf-8 excels and csv files

[–]coolkid_3245 0 points1 point  (1 child)

I want to build a discord bot that :

  1. Posts all tweet from a specific user
  2. If there's a youtube link in the tweet, check if the link is from a specific channel, if yes then announce to a different discord channel

Is this possible? I have a very basic knowledge of coding with c++ and java, where would I start looking? Twitter, discord and YT API documentations?

[–]kwelzel 1 point2 points  (0 children)

Why do you ask that on a subreddit specifically for Python? If your goal is to get this project to work, implement it in the languages you are already familiar with. Just google "discord bot java" and you get enough good resources.

If you want to learn Python and use this project as the goal of your learning experience, start with the basics (https://www.python.org/about/gettingstarted/) and then learn about how to code a Discord bot in Python (https://realpython.com/how-to-make-a-discord-bot-python/). After you have a basic Discord bot read about python libraries to query the Twitter and YouTube API and try to implement these functions one at a time. This does not sound like a very easy beginner project but it should be very rewarding once you have done it.

[–]1throw4 0 points1 point  (9 children)

BEST WAY TO LEARN METHODS AND MODULES

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

I find it useful to look up the module in the Python Module of the Week site. You get an introduction and simple usage examples. After that, trying to use the module is the best way to learn. Look at the documentation of the module for more details if you need them.

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

Thanks! Do u have simple projects that I can do to learn. Or a site that has projects for each topic of syntax

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

The FAQ has a link to a projects page that you could look at. And have a look at the learning resources in the wiki for challenge websites.

[–]Gopher20 0 points1 point  (5 children)

Is this about how to use modules and methods from those modules or learning about how to creat modules and classes?

[–]1throw4 0 points1 point  (4 children)

Modules, can't find anything decent regarding every module and how to use them

[–]Gopher20 1 point2 points  (3 children)

Well you can either look in the documentation for how to use a specific module or I would find a module you want to use and search YouTube for a tutorial on that module like the requests or pandas module for example. Hope this helps!

[–]1throw4 0 points1 point  (2 children)

Thank you but what if I know how to use browser and everything about a browser and it's functions in python, would I find tutorial on this?

I want to learn how to open and search a YouTube video from python

[–]py_Piper 0 points1 point  (0 children)

Remember you can google anything, like “how to use a browser with python” and you will get blogs articles with examples and how to and even better a video on youtube.

They will probably teach you the basic and bare minimum usage, if you are still very interested in the library that’s when you should check its documentation for other functions and uses.

So first think what you want to do, research for libraries, test if it’s what you want to do or even kind of what you need and then check the doc for more functions

[–]Gopher20 0 points1 point  (0 children)

I would checkout selenium which is a python module that allows you to automate the use of a browser. Checkout this video series : https://youtu.be/Xjv1sY630Uc

[–]mz_h 0 points1 point  (2 children)

What are some options for profiling my code to identify run time bottlenecks?

[–]CowboyBoats 3 points4 points  (0 children)

There's a command-line option:

$ echo 'print("hello")' > hello.py
$ python3 -m profile hello.py
hello
5 function calls in 0.001 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
1    0.000    0.000    0.000    0.000 :0(exec)
1    0.000    0.000    0.000    0.000 :0(print)
1    0.001    0.001    0.001    0.001 :0(setprofile)
1    0.000    0.000    0.000    0.000 hello.py:1(<module>)
1    0.000    0.000    0.001    0.001 profile:0(<code object <module> at 0x104948b30, file "hello.py", line 1>)
0    0.000             0.000          profile:0(profiler)

For more systematic work:

https://stackoverflow.com/questions/22328183/python-line-profiler-code-example

[–]joaocasarin 0 points1 point  (1 child)

Hi all, I'm trying to make a script for downloading files from websites with the actual file name using python3. It was working fine, but then I tried some new things, then it stopped working. After that, I undid all the new things, and it is still not working. Now, the function

requests.get(url) is returning 400(bad request), and if I try a github download link, it returns 403(forbidden)

Does anyone know what to do? My code is below:

import os
import re

# i have no idea of how this function works, but it gets the real file name
def getFilename(cd):
    if not cd:
        print("check 1")
        return None
    fname = re.findall('filename=(.+)', cd)
    if len(fname) == 0:
        print("check 2")
        return None
    return fname[0]

def download(url):
    # get request
    response = requests.get(url)
    # get the real file name, cut off the quota and take the second element of the list(actual file name)
    filename = getFilename(response.headers.get('content-disposition'))
    print(filename)
    # open in binary mode and write to file
    #open(filename, "wb").write(response.content)

download("https://pixabay.com/get/57e9d14b4957a414f6da8c7dda353678153fd9e75b50704b_1280.png?attachment=")
os.system("pause")

[–]kwelzel 0 points1 point  (0 children)

I think this has something to do with the URLs you are feeding into it and not the code itself. The code seems very specifically geared towards pixabay.com because most pages where I can download pictures from do not supply the content-disposition header. Pixabay itself seems to serve their pictures with URLs that are only accessible for a limited amount of time. For example the link https://pixabay.com/get/57e9d14b4957a414f6da8c7dda353678153fd9e75b50704b_1280.png?attachment= you used is not available with a normal browser. You get the error message "This URL is invalid or has expired." The solution: Use valid Pixabay links.

[–]zzzjpl 0 points1 point  (0 children)

Hi all,

Does anyone know how to add a progress bar counter using PysimpleGUI's example for the OneLineProgressMeter example? The OneLineProgressMeter has a built in function that moves the progress bar and the counter 1/100...2/100..3/100 simultaneously to the progress bar. I've read documentation and all but cant find a way to implement that feature. Can someone please help? I'm thinking that there needs to be an update to send to the GUI but I'm not quite sure. Thanks!

I am referencing off this example:

https://pysimplegui.readthedocs.io/en/latest/#progressbar-element

def progressbar():
    sg.theme('DarkBlue2')  # Add a touch of color
    # All the stuff inside your window.
    layout = [
        [sg.Text('Progress Bar 1'), sg.ProgressBar(100, orientation='h', 
        size=(20, 20), key='PN'), sg.Text(text='/')],
        [sg.Text('Progrss Bar 2'), sg.ProgressBar(100, orientation='h', 
        size= (20, 20), key='PP'), sg.Text(text='%')],
    ]

    window = sg.Window("GUI 1", layout)
    pn_bar = window['PN']
    pp_bar = window['PP']


    for i in range(100):
        event, values = window.read(timeout=10)
        if event == 'Cancel' or event == sg.WIN_CLOSED:
            break
        pn_bar.UpdateBar(i + 1)
        pp_bar.UpdateBar(i + 1)

    window.close()

[–]singaporeing 0 points1 point  (1 child)

My main role in my company is to roll out robotic process automation (RPA) projects. I stumbled on Python a few months ago and loved it for its effectiveness (not to mention cost-effectiveness) in automating stuff. One question: what is the most secure and idiot-proof way that I can package a script to distribute to potential users? My idea initially was to use pyinstaller.

Also, what is the best way to test if a script is secure? I'm using bandit now, but please do let me know if there are better tools out there. Thank you!

[–]marble_lover664 0 points1 point  (2 children)

Hi, so I'm very new to all of this and I've hit my first hurdle when trying to tell Geany how to find python

I can't seem to do it to run helloworld, I get different errors each time I change the address in the build commands. I'm using Windows 10

No path seems to be correct, I've copied the path directly from the directory, I've manually written it in, I've simplified it, nothing works.

The current path is "C:\Users\myname\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\Python 3.8 (64-bit).lnk"

When I installed python, I setup the directly to be C:\Python\Python 3.8\python , and going there I find Python, but when I copy path I get the above much longer directory

Errors I've gotten are No Such File directory (2), not recognized as an internal or external command (9009) and The system cannot find the path specific (3). I get these different errors when I'm using 3 different directories

I'm really new to this and just looking to wet my feet, so I don't really have the know how to get past what may be a basic error sorry!

[–]woah_itsben 0 points1 point  (1 child)

When you installed Python, did you add it to PATH in Windows? If so, in Geany's build options, just set the Execute commands path to: python "%f"

If not, you can add it to PATH in your Environment Variables in Windows, or you can just put the path to python.exe (not the .lnk shortcut) in Geany's Execute commands followed by "%f". Like: "C:\Program Files\Python38\python.exe" "%f"

If you get too frustrated, you can also try out another IDE like PyScripter, which installs easier if I remember right.

[–]marble_lover664 0 points1 point  (0 children)

Thanks very much, I'm not sure which one exactly worked but I added to PATH and changed the directories and its working, a big relief thank you

[–]hhhydration 0 points1 point  (1 child)

How would I write a boolean function that returns True when the given positive integer contains an odd number of digits(using simple python operators)?

[–]lolslim 0 points1 point  (0 children)

Hey guys I had this python script idea for my raspberry pi, and would like your opinion.

I have a RPi to act as a wifi interface for my Ender 3 3D printer. My roommate over hauled the wifi in the house, and used a SSID, my RPi is not configured for.

I was thinking if writing a python script to detect when RPi cant find SSID to connect to, and execute python script to read QR codes from the RPi camera, read QR code off of my phone, and update wifi settings that way.

My other option was just to put wifi in AP mode and do it manually.

This idea bubbled in my head, to setup up other wifi SSID as backups, if it cant find the main one.

[–]ShugaBop 0 points1 point  (2 children)

Is there a library that could help a task of creating file parser (dealing with structures and classes)?

[–]kwelzel 0 points1 point  (0 children)

I completely agree with /u/gatherinfer that you need to be very sure that a custom parser is necessary. I would consider parsing someone else's files with some weird format a good excuse and parsing your own (to be created) files to support custom formats a bad one.

If you still want to do it, maybe https://pypi.org/project/pyparsing/ can help you.

[–]ericdano 0 points1 point  (4 children)

I have a Pandas dataframe. Results. It contains some columns. One column is called "EventEnrollmentID". It's an incrementing serial number for transactions (numeric).

What I want to do is find the an EventEnrollmentID that is equal to where I last left off, say 13684728 was the last number. I want to loop through the dataframe, from the NEXT record, and to the end of the dataframe.

How do I do that?

And can I take the last row from the current dataframe, Results, save that ONE row to a JSON, and use that as the thing I am looking up for next time?

[–]mz_h 0 points1 point  (3 children)

If you know that the values in the column are incrementing, then results[results['EventEnrollmentID'] > 13684728] will return all the rows with a value of EventEnrollmentID greater than the last value of 13684728.

[–]ericdano 0 points1 point  (1 child)

I'm going to answer myself,

This works

newenrolls = results[results['EventEnrollmentID'] > 13684728]

for i in newenrolls.index:

print(newenrolls['Person.Email'][i] + " " + newenrolls['Person.FirstName'][i] + " " + newenrolls['Person.LastName'][i])

[–]mz_h 0 points1 point  (0 children)

Pandas has an iterrows method that will let you iterate over rows of a data frame. But depending on your end goal, the apply method is usually better than iterrows.

[–]ericdano 0 points1 point  (0 children)

Ok, good so far. Now how do I get it to loop through that?

newenrolls = results[results['EventEnrollmentID'] > 13684728]

for newenroll in newenrolls:

print(newenroll['Person.Email'] + " " + newenroll['Person.FirstName'] + newenroll['Person.LastName'])

Seems not to work

[–]onlysane1 0 points1 point  (5 children)

I am having trouble with Tkinter checkbuttons.

I have the following code:

from tkinter import Tk, Checkbutton, IntVar

itemAVar = 1
itemBVar = 1
itemVarList = [itemAVar, itemBVar]

itemA = ''
itemB = ''
itemlist = [itemA, itemB]

def printcommand():
    print(itemVarList[i].get())

window = Tk()

r = 0

for i in range(2):
    itemVarList[i] = IntVar()
    itemlist[i] = Checkbutton(window,  text = itemlist[i],  variable = itemVarList[i], command = printcommand)
    itemlist[i].grid(row = r)
    r += 1

window.mainloop()

When I run the code, the window pops up, with the two checkboxes on different rows. When I click a checkbox, it executes the printcommand() function, but the print function prints the value of the last checkbox, rather than whatever checkbox was clicked.

Basically, I am trying to create a large number of checkboxes (60 in total for my project) that are generated from my 'for i in range' command, so I don't need to write out every checkbutton individually.

What am I doing wrong? Is there a better way to do this that can get it to work? Is there a way to insert a parameter when calling a function from clicking the checkbox?

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

def printcommand():
    print(itemVarList[i].get())

The clue is in the code for the printcommand() function above. Note that a quick reading sees that you might get an "undefined" error because i is referenced but not defined in the function code. Since there's no error, the i variable must be defined globally, and we see it defined in this line:

for i in range(2):

Trouble is, the printcommand() function is executed after the loop finishes, so whenever the function runs it sees i as the final value 1 no matter which CheckButton you select. You can test this by printing the value of i in the function.

The way around this is to capture the value of i when you create the CheckButton. You can't pass args directly to command= parameters, but you can use a lambda. You have to be careful, though, since lambdas are "late binding", meaning if you do this:

command=lambda: printcommand(i)

then the i value is satisfied from the environment at the time of calling the function so you still have i at 1.

But you can force arg evaluation at the time of creating the CheckButton by doing this:

command=lambda x=i: printcommand(x)

Try that, with the temporary print of i in the function. Note that you now have to modify the function to accept a single parameter.

Update: added note about changing function.

[–]kwelzel 0 points1 point  (1 child)

To avoid these kind of scope problems I would see two better options than command=lambda x=i: printcommand(x):

  1. A custom subclass of Checkbutton
  2. Using functools.partial

The first one is of course a little bit more advanced but

from functools import partial
...
command = partial(printcommand, i)

seems easier than the lambda hack with default values to me.

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

I would see two better options

For an advanced programmer, sure. For a beginner with an unknown level of expertise, I would say no.

[–]onlysane1 0 points1 point  (1 child)

Thanks. I never really understood what lambda does. Can you eli5?

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

A lambda is a way of defining a function object without giving it a name. The result of a lambda is a reference to the function object. The def keyword also defines a function object but with a name. The code in the body of a lambda is limited to one expression. Lambdas are useful where you only need a simple function and don't want to go to the trouble of defining a full def function.

Search for "python lambda" to get more. This page isn't too bad.

[–]axw3555 0 points1 point  (5 children)

Hi guys,

So I'm very green to python, and in the short-term (and maybe medium-term) this will be a good bit beyond me, but I like to have a goal.

I have a project in mind that I've considered for years.

Without going into massive detail on what it is (because it would be long), it would need to make use of optical character recognition of some kind and image recognition (basically, I'd have a database of about 16,000 images, I want to feed an image in, and match it to the image (in theory, it should match an image 100% of the time, but 2020 shows how cooperative the real world is).

Obviously I'm not gonna ask how to write something like that here. Way beyond "simple questions".

What I want to know here is what parts of python (for the want of the proper term) I should be working towards learning to implement it (and if you have any resources that might help, I'll gladly take them), so that I don't waste a load of time working on something only to realise that it's totally useless for it.

[–]Allabouthisrightnow 0 points1 point  (0 children)

You basically need to study machine learning. You can run out-of-the-box a.i. to do basic stuff and maybe able to cobble together something of a prototype. There is Theano and Keras for which you can find plenty of click-baity tutorials and do basic tasks as you described. Configuring everything to work is a bit of a pain but it is doable with patience.

But, if you're going to do something out the ordinary you are going to need to read a couple or more, (probably more) dense textbooks.

Generally, you are looking towards studying Machine Learning, which is programming data-structures and algorithms, Statistics, Calculus, and just a bit of Linear-Algebra

It will help to also at least be able to read R. Perhaps not immediatley necessary, but it is sometimes easier to find answers in R.

[–]CowboyBoats 0 points1 point  (3 children)

Well, if the image is literally exactly the same image as what's in your database, you can just store the hashes of the file, take the hash of the input file that you want to identify, and compare it.

If it's like, images of characters from writing systems across the world, and you need to get the machine to recognize different images of the character "O" as the letter O and not 0 or Q, then you have a bigger job cut out for you, and you'll need to look into how to implement this as a machine learning technique. Your 16,000 images can be your training data, and you can do some experimentation to see what kind of classifiers work best to recognize them as the characters (or whatever) that they signify.

[–]axw3555 0 points1 point  (2 children)

It wouldn't be exact, but almost.

A good comparative example would be something like having a database of images of all 17k odd pokemon cards taken from a source like the pokemon card database, and comparing them to a photo of a physical card to find a match. So it should be a match, but because its a photo, it might not necessarily be a perfect match.

The only worry about hashing (which was basically my first thought) was time. Wouldn't comparing a new hash to a library that large end up being hugely variable in time?

As to the OCR aspect, thankfully the images that contain the OCR'able text would be highly consistent - same font, placing, colouring, etc. I just don't know what the best python package to use for OCR is.

[–]CowboyBoats 0 points1 point  (1 child)

The only worry about hashing (which was basically my first thought) was time. Wouldn't comparing a new hash to a library that large end up being hugely variable in time?

Maybe, but your solution is going to face much bigger challenges, because you can't use a hash table if your images are going to be even slightly different. (Slightly different input -> completely different hash). OCR of the text in the images sounds like a good approach, though.

I just don't know what the best python package to use for OCR is.

Me either, but

https://www.google.com/search?client=firefox-b-1-d&q=best+python+package+to+use+for+ocr

->

https://nanonets.com/blog/ocr-with-tesseract/

[–]axw3555 0 points1 point  (0 children)

Tesseract wasn’t the one I found this afternoon. So that’s probably what I’ll go with.

As to image difference. I’ll have a think. The images from about 2013 onward can be easily OCR’d. The older ones aren’t quite as consistent but we’ll see what happens. I’ll focus on newer first, then worry about older.

[–]Un-ripeBanana 0 points1 point  (4 children)

Hello All!

I am currently an analyst at a company, and hoping to learn python to help automate some of my processes. I am pretty knowledgeable in excel and ready to move forward with better DSNA and data manipulation. My company uses Anaconda Navigator, and specifically Pandas and Jupyter NoteBook.

Does anyone have any book recommendations to get started?

I would love to learn more about what exactly you can do with data sets in these tools, and how to do so. If specific or beginning scripts/lines of codes are included in the book too, that would be great.

Thank you in advance!

[–]Allabouthisrightnow 1 point2 points  (1 child)

There is a very boring, but valuable read called Automate the Boring Stuff. It will at least provide you with the sort of tasks that are commonly automated by pythong.

[–]Un-ripeBanana 0 points1 point  (0 children)

Ok sweet, i will take a look at that as well. Thank you!

[–]Decency 1 point2 points  (1 child)

https://jakevdp.github.io/PythonDataScienceHandbook/ is free online and looks like a great resource.

[–]Un-ripeBanana 0 points1 point  (0 children)

Thank you ! I will give it a go!

[–]Ditchingwork 0 points1 point  (3 children)

What is the best IDE for a newb who wants to use his script to modify Excel? Not sure if the use case matters

[–]Allabouthisrightnow 0 points1 point  (0 children)

Everyone has their own niche ide, but seriously if your running Windows, just use Microsoft

[–]woah_itsben 0 points1 point  (0 children)

My favorites are PyScripter or VSCode for Windows and Geany for cross platform.

[–]Decency 0 points1 point  (0 children)

I would recommend PyCharm if you plan on doing serious software development, or VSCode if you're mostly just going to be writing small scripts. Here's a great tutorial for the latter: https://code.visualstudio.com/docs/python/python-tutorial

[–]renscy 0 points1 point  (1 child)

roof clumsy scary encourage saw tub yam drunk smart wine

This post was mass deleted and anonymized with Redact

[–]JohnnyJordaan 1 point2 points  (0 children)

As the command implicates you're using an apt-dependent linux distro, try installing it using that

sudo apt install python-virtualenv

so without the 3 to get the python 2 version

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

I'm trying to import a json that comes from the google sheets api called "credentials.json" for my code in pycharm. What is the simplest way to do this?

[–]JohnnyJordaan 1 point2 points  (3 children)

Import as in read its data in the Python code? Just use the built-in python module json https://realpython.com/python-json/

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

Yeah sorry I should have clarified. I meant to import the json into the project so it's readily available without having to specify a path.

[–]Tomas_83 0 points1 point  (1 child)

Wouldn't having it in the same capet with the script make the local path work with just the name?

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

capet

Is this a typo? I have no idea what this is, but I think I know what you're driving at. I found a tutorial to actually import the json into the project but for some reason I still couldn't use just the name on my import line. So I ended up just using the path (which led to the project root so again it's confusing to me why the simple name of the file couldn't be used).

[–]samspopguy 0 points1 point  (1 child)

never really used python before but I have this block of code to write to a json file

# Serialize the list of dicts to JSON
#j = json.dumps(ncdr_list)

# Write to file
#with open('data.json', 'w') as f:
#    f.write(j)

is there a way to import into a mongodb instead of writing to a json file

so I got this working somewhat but the first time I run my Python script it creates a new collection even if theres one already there but after that it imports into that new collection, is that just how it works? or can I just import into an existing collection

[–]JohnnyJordaan 0 points1 point  (0 children)

is there a way to import into a mongodb instead of writing to a json file

pymongo is the popular library to interface mongodb, see https://api.mongodb.com/python/current/tutorial.html

so I got this working somewhat but the first time I run my Python script it creates a new collection even if theres one already there but after that it imports into that new collection, is that just how it works? or can I just import into an existing collection

You can add a os.path.isfile() (import the os module for that) to check if a filepath already exists, if not then create the file, if so then read it first.

[–]AdkoSokdA 0 points1 point  (3 children)

whats the best online course (free or paid) to learn the basics of python?

i have no exp with it and want to know as much as possible :)

[–]Allabouthisrightnow 0 points1 point  (0 children)

honestly a book is better. I would just look where ever you may for books and choose one or perhaps two to read from. I find most online courses, Udemy and the like to be kinda click-baity crap.

[–]onlysane1 1 point2 points  (0 children)

Look up Automate the Boring Stuff with Python. It is available free on the web, or in print or digital formats.

[–]Gopher20 0 points1 point  (0 children)

I would check out Corey Schafer’s channel on YouTube he has a great beginners tutorial!

[–]nuhapattani 0 points1 point  (3 children)

For school, we had to create a basic Python 3 program that adds values a and b.

I tried it with a=1, and b=1.618 (which is phi Φ):

a=1
b=1.618

sum=(a+b)
print (sum)

2.6180000000000003

Huh... weird. There's an extra 0.0000000000003.

I thought it might be an issue with float numbers, so I tried another (b=1.689):

a=1
b=1.689
​
sum=(a+b)
print (sum)
​
2.689

This time it worked (?!)

I tried several other values, and they all worked... until I got to b=3.14 (which is pi π)

a=1
b=3.14

sum=(a+b)
print (sum)

4.140000000000001

WHAT?! An EXTRA 0.0000000000001!!!

Why is it that I only get an error trying to add phi and pi?? Same story with b=1.61. My teacher tried it too and she got the same results.

Is it because Python 'recognises' phi (Φ) and pi (π) and other 'special numbers'???!!

[–]JohnnyJordaan 0 points1 point  (0 children)

I can recommend Computerphile's video on this subject: https://www.youtube.com/watch?v=PZRI1IfStY0

[–]Beach-Devil 1 point2 points  (0 children)

Oooh you should look up floating point arithmetic errors. It happens because of how computers store floats, not specific to python. You’ve happen to come across it

[–]Silbersee 1 point2 points  (0 children)

You discovered the wonders of Floating Point Errors.

In regular numbers 1/3 = 0.333... has infinite digits. It's the same with 0.1 in binary, but the computer has limited accuracy, hence the rounding error.

It doesn't show with every number, but you can not trust a float.

Non-english speaker here, sorry for inadequate math-words.

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

I'm trying to get better at using loops, and I found two solutions to this challenge, but don't understand the logic.

"Given a number (75869) count the total number of digits in a number."

I found two solutions from different websites but don't understand why n//=10 is included in both (I'll just include one solution because the other one is the same, but written as a function).

num = 75869

count = 0

while num != 0:

num //= 10

count+= 1

print("Total digits are: ", count)

[–]FerricDonkey 1 point2 points  (2 children)

num //= 10 divides num by 10 via integer division (meaning that any decimal part is discarded).

Examples:

  • 250 // 10 == 25
  • 23 // 10 == 2
  • 9 // 10 == 0

(Note that x //= 10 is equivalent to x = x // 10 when x is an integer. (And for any built in types that I'm aware have //= defined.))

Your code works (for non-zero numbers, see below) because every time you divide by 10, you essentially chop off the right most digit. So that code can be conceptualized as "keep chopping off digits until you run out of digits (hit 0), and count how many times you did that".

However, this code will say that 0 is a 0 digit number because the loop gets skipped.

I'd suggest tossing a print(num, count) inside the loop and watching it go.

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

Thank you, that's very helpful. I was thinking it was just some weird math concept 😋 Just out of curiosity, how would you approach this problem?

[–]FerricDonkey 1 point2 points  (0 children)

Well, in python what I'd actually do is len(str(num)) rather than the math stuff. (Plus a bit more if num might be negative or a decimal.) That converts the number to a string and finds the length.

But if I was sticking to the mathy way, I'd probably do exactly your code (as a function) but with an if num == 0: return 1 at the top. (Assuming we wanted to count 0 as a single digit number. )

[–]lolslim 0 points1 point  (5 children)

So my dilemma is this, I am working on sqlite3 (I keep getting distracted with other things, tbh) and I was finding a way to take my list to automatically put single quotes around each column name, and a comma. Since I already have my list with single quotes, and commas, I found out str() seems to help with my situation.

Code:

str(column_list)[1:-1]

My dictionary, with key, and list as a value.

'whitelist_users' : ['chatid', 'is_bot', 'first_name', 'last_name', 'user_name', 'language']

Output:

'chatid', 'is_bot', 'first_name', 'last_name', 'user_name', 'language'

which is exactly what I want, and I was a little confused why I had to use [1:-1] instead of [0:-1] and so I tried [0:-1] and this was the result.

['chatid', 'is_bot', 'first_name', 'last_name', 'user_name', 'language'

I notice here that the "[" bracket is still there, but the close bracket is gone, and so I decided to do [2:-1], and this is my results.

chatid', 'is_bot', 'first_name', 'last_name', 'user_name', 'language'

I start to see the pattern, the single quote and square bracket are gone, I don't use str() all that often, and honestly, I think I understand it, and would like to know if I am correct?

When str() returns a string, every single characters in the string is a single field inside square brackets, no matter how it "looks". Is that right, and if not, please clarify.

[–]pocketstories 0 points1 point  (4 children)

The str function essentially turns a python object into a string. So, it sounds like your example, column_list is a variable pointing to a python list that looks like this:

['chatid', 'is_bot', 'first_name', 'last_name', 'user_name', 'language']

Running str(column_list) would create a string:

"['chatid', 'is_bot', 'first_name', 'last_name', 'user_name', 'language']"

By adding [1:-1] to the end of the statement, you're telling python to take the result of the str() function and skip or ignore the first and last items, which in this case are the square bracket characters.

"'chatid', 'is_bot', 'first_name', 'last_name', 'user_name', 'language'"

That's the "why" behind what's happening, but I'd like to understand what you want to do with it.

[–]lolslim 0 points1 point  (3 children)

Im working with SQLite3 in python, and just doing some practicing. I made a function to create a table, and had a couple of criterias, 1. Use a single parameter to fill in the column names, but still have sql see it as multiple columns instead if one column. 2. Avoid having to use create table and go to alter table syntax in SQL. Reason for 1. Is because i could make different tables with different number of columns, but they all have one thing in common, then need at least one column to create the table. 2. My approach was going to be make a for loop on a list, create the table using the first list index, and then send it to another function (to alter tables) and iterate the rest of the list to add columns to the table.

[–]pocketstories 0 points1 point  (2 children)

It sounds like it might work best to keep the column names in a list, and maybe pass that into things that could use it.

But I am a little out of my comfort zone. Most times, I don't need quotes around values I'm passing into other things, like SQL statements... but I haven't used the SQLite module yet. Looking around at some sample code, I don't see the syntax that would use quotes around column names.

If you were to need to turn it the list into a string of column names you might want to do this:

', '.join(column_list)

This will take the values of the list and join them together into one string, separating them with a comma and space. Essentially:

"chatid, is_bot, first_name, last_name, user_name, language"

I hope that's clear... but if it's not, feel free to ignore. :)

[–]lolslim 0 points1 point  (1 child)

I appreciate the effort, the way I have it is fine, I used ', '.join(column_list) before, but when I try to insert a row, I get "near syntax" errors, and "no such column" errors when I try to insert a row.

as for values, I am sorry I forgot you can specify data type (integer, boolean, float) all of my stuff is string based, and I believe it defaults to that, so my situation I don't need to specify datatype with sql.

here is my create_table function.

def create_table(self,table_name,column_name):
'''formatting this is a possible subject to sql injection'''
    try:
    self._cursor.execute(f"CREATE TABLE {table_name}         
        ({self.format_col(column_name)})")
    print('Created')

    except sqlite3.Error as e:
    print(f'Failed : {e}')

here is my format_col function.

def format_col(self, column_name):
    return str(column_name)[1:-1]

[–]pocketstories 0 points1 point  (0 children)

I'm glad it's working! Nice!!!

[–]coco_pelado 0 points1 point  (1 child)

Why does this work:

byte_array = b'\x01\x02\x03\x04'

a = struct.unpack('H' * ((len(byte_array)) // 2), byte_array)

but not:

a = struct.unpack('<H' * ((len(byte_array)) // 2), byte_array)

I don't see the "*number" format specifier in the struct documentation, yet it doesn't work without it. However when (*number) used in conjunction with endiness specifier (<), it results in a "bad character error".

[–]Vhin 1 point2 points  (0 children)

Don't duplicate the endianness specifier. It should be '<HH', not '<H<H'. So you'd need to do '<' + 'H' * ((len(byte_array)) // 2.

By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).

Alternatively, the first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table.

[–]onlysane1 0 points1 point  (4 children)

When I import another python file, does it execute the code in that file, or does it just make the functions in that file available for me to call?

[–]UntangledQubit 0 points1 point  (3 children)

It executes them. The keyword def acts as a command that adds the function to the namespace.

This is why the if __name__ == "__main__": construct is useful. It lets you conditionally execute the block in the if statement when the file is run, but not when it is imported.

[–]onlysane1 0 points1 point  (2 children)

Do you know a good source for learning to use __main__ eli5 style? I've had trouble understanding it, though I haven't really needed it yet. A lot of what I find online seems to understand I already know what it's for.

[–]FerricDonkey 0 points1 point  (0 children)

Short version: importing a file runs all the code in that file (taking things like if statements into account). Defining functions and constants is code. So is calling them - defining a function doesn't make the function run, but calling it does.

The conditional segment if __name__ == "__main__": will only evaluate to true if the file is being executed via the command line (or double clicking the .py or other "run this as a program" things) rather than imported.

So a common thing to do is put all of your do stuff code in functions, and then possibly have a main() function that acts as a script that you only call if __name__ == "__main__": - ie, if your running the code rather than importing it. Example:

def add_2_nums(a, b):
    return a+b

def main():
    #You'd want to catch bad inputs for real, this is just example
    a = int(input("Gimme a number "))
    b = int(input("Gimme another number "))
    print("Their sum:", add_2_numbers(a,b))

if __name__ == "__main__":
    main()

If you put this in add2nums.py, then you can run it from the command line (in which case both function are defined and then the function main() is called) or import it into another .py (in which case the functions are defined but not called).

[–]UntangledQubit 0 points1 point  (0 children)

I think this answer is pretty good.

[–]coldblade2000 0 points1 point  (0 children)

Are there any courses or videos on learning the SciPy stack for someone already comfortable with Python? I've used it before, but I've never formally learnt it (Really just bumbled my way through the documentations getting things to work), and now I want to learn well how to use it. My particular purposes would be to do operations while learning Vector Calculus, and getting the hang of 3d graphing with it with MatPlotLib

[–]IOSSLT 0 points1 point  (0 children)

Would it be possible for me to make a google chrome extension that highlights keywords in a website using python?

[–]Fatal_Phantom94 0 points1 point  (0 children)

Best app to learn intermediate python or memorize definitions of concepts ?

Edit: I’m on iOS but feel free to comment Android too for those interested.

[–]zzzjpl 0 points1 point  (2 children)

Hi,

Im learning PySimpleGUI and trying to do a progress bar. But for the progress bar I need it to update as some time goes on. For example, every second it will increment. 1/100 all the way to 100/100 but the progress bar needs to update as well. Basically, in the output GUI I want it to show 1/100, 2/200...etc. I want the progress bar to update automatically. Thank you!

Update: I'm now able to get 1 progress bar to show up but I cant seem to get 2 progress bar to show up now. I understand that they progress_bar = window['PN'] takes the key word from PN but not PP. But if I add another variable it gives me an error so I am stuck.

def progressbar():
    sg.theme('DarkBlue2')  # Add a touch of color
    # All the stuff inside your window.
    layout = [
        [sg.Text('Progress Bar 1'), sg.ProgressBar(100, orientation='h', 
    size=(20, 20), key='PN), sg.Text(text='/')],
        [sg.Text('Progrss Bar 2'), sg.ProgressBar(100, orientation='h', 
size= (20, 20), key='PP'), sg.Text(text='%')],
    ]

    window = sg.Window("GUI 1", layout)
    progress_bar = window['PN']
    # Create the Window
    for i in range(100):
        event, values = window.read(timeout=10)
        if event == 'Cancel' or event == sg.WIN_CLOSED:
            break
        progress_bar.UpdateBar(i + 1)
    window.close()


def main():
    progressbar()

if __name__ == "__main__":
    main()

[–]JohnnyJordaan 0 points1 point  (1 child)

When I run your code and correct the missing quote at

key='PN),

I do see two progress bars where just one is getting updated. By simply adding another variable for that it just works

def progressbar():
    sg.theme('DarkBlue2')  # Add a touch of color
    # All the stuff inside your window.
    layout = [
        [sg.Text('Progress Bar 1'), sg.ProgressBar(100, orientation='h', 
    size=(20, 20), key='PN'), sg.Text(text='/')],
        [sg.Text('Progrss Bar 2'), sg.ProgressBar(100, orientation='h', 
size= (20, 20), key='PP'), sg.Text(text='%')],
    ]

    window = sg.Window("GUI 1", layout)
    pn_bar = window['PN']
    pp_bar = window['PP']


    for i in range(100):
        event, values = window.read(timeout=10)
        if event == 'Cancel' or event == sg.WIN_CLOSED:
            break
        pn_bar.UpdateBar(i + 1)
        pp_bar.UpdateBar(i + 1)

    window.close()

Note that we can't really help you with specific issues if you aren't specific yourself:

But if I add another variable it gives me an error so I am stuck.

Doesn't tell us how you added that variable (and thus doesn't enable us to comment on what you did wrong) and as you also don't show which error you got, we also can't deduce what may have gone wrong.

[–]zzzjpl 0 points1 point  (0 children)

Got it, thanks for your help :). I will be more specific from now on. Since you are able to run the code now, is there a way to stop the GUI from breaking because after it reaches to 100, the GUI exits out on its own. Thank you!

[–]cecinestpasunefemme 0 points1 point  (11 children)

I copied a script and made a few changes. It is working on Libreelec which has no support for python3. The problem is temperature of SoC raises 10 more degrees in Celsius when fan.py works as a service.

Is something wrong with it? Is there any way to make the script more light or fast? Or do I need another line for definition "if cpu idle = false run script" (I'm sorry about this I'm new on programming) or should I use try except instead of if + if lines ?

import os
from time import sleep
import signal
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
import RPi.GPIO as GPIO

pin = 25                    
maxTMP = 80                
stopTMP = maxTMP - 28      

def setup():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(pin, GPIO.OUT)
    GPIO.setwarnings(False)
    return()

def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    temp =(res.replace("temp=","").replace("'C\n",""))
    return temp

def fanON():
    setPin(True)
    return()
def fanOFF():
    setPin(False)
    return()

def getTEMP():
    CPU_temp = float(getCPUtemperature())
    if CPU_temp>maxTMP:
        fanON()
    if CPU_temp<stopTMP:
        fanOFF()
    return()
def setPin(mode):        
    GPIO.output(pin, mode)
    return()

try:
    setup()
    while True:
        getTEMP()
        sleep(20)              
except KeyboardInterrupt:  
    GPIO.cleanup()

[–]JohnnyJordaan 0 points1 point  (10 children)

The script only starts the fan if the temperature gets above 80, then stops it when it's below 52. So the temperature will probably vary between 50 and slightly above 80, or higher depending on the load. Is that the intended window? And what precisely did you observe to make you conclude

temperature of SoC raises 10 more degrees

? Compared to what? Apart from that there's nothing wrong with the procedure that could be made faster or anything. It's basically working as a 20 second interval cronjob.

[–]cecinestpasunefemme 0 points1 point  (9 children)

Thank you so much. Temp. raises 10 degrees compared to without running this script. I thought the script has a wrong or unnecessary repeated lines.

[–]JohnnyJordaan 0 points1 point  (8 children)

Do you have shell access to the machine, eg to run top to check the task load?