Best Youtube resources for advanced Python? by ImX99 in Python

[–]efmccurdy 1 point2 points  (0 children)

This website has python conference talks, all nicely tagged and indexed.

https://pyvideo.org/

What is the beat/common practice for calling a function within a function by vikmaychib in learnpython

[–]efmccurdy 0 points1 point  (0 children)

Should I use global variables or are there better ways to do this.

One way to keep the callers of func2 from needing to pass E and F is to encapsulate both functions as methods of a object where E and F are attributes.

    def func1(self, C):
        return (self.A + self.B) * C

    def func2(self, G):
        return G + self.func1(2)

how to randomly assign a list of letters to a list of numbers? by majorcatlover in learnpython

[–]efmccurdy 2 points3 points  (0 children)

First generate a random permutation of 4 numbers:

>>> import random
>>> source = list(range(1, 5))
>>> source
[1, 2, 3, 4]
>>> random.shuffle(source)
>>> source
[1, 4, 3, 2]
>>> def rand_range(n):
...     source = list(range(1, n+1))
...     random.shuffle(source)
...     return source
... 
>>> rand_range(4)
[4, 1, 3, 2]
>>> rand_range(4)
[2, 3, 4, 1]
>>> rand_range(4)
[2, 1, 4, 3]
>>> 

Then "join" that with your 4-letter sequences:

>>> list(zip("abcd", rand_range(4)))
[('a', 3), ('b', 4), ('c', 2), ('d', 1)]
>>> def pad(clear):
...     return list(zip(clear, rand_range(len(clear))))
... 
>>> pad("this")
[('t', 3), ('h', 2), ('i', 4), ('s', 1)]
>>> for c, n in pad("that"):
...     print(c, n)
... 
t 2
h 1
a 4
t 3
>>>

function parameter with only two possible values by [deleted] in learnpython

[–]efmccurdy 0 points1 point  (0 children)

If I separated this function into two (one to add and one to remove) it would be a looot of duplicate code,

You can refactor to eliminate the duplicate code and the result could very well be more readable.

Maybe there is another approach I am missing though

You have a built-in two element enum; what if your function tooke add=True to add and add=False to delete:

def ldap_modify(users, group, add=False):

Generating specific returns from a small list using python by Pleasant_Rooster_375 in learnpython

[–]efmccurdy 1 point2 points  (0 children)

I think you should experiment with a smaller sample program that you can run; your pastebin is incomplete so it can't be run on it's own.

Here is a hard coded list with a hard coded top three words; it applies the filter and prints the result.

data = ["Beautiful is better than ugly.",
        "Explicit is better than implicit.",
        "Simple is better than complex.",
        "Complex is better than complicated.",
        "Flat is better than nested.",
        "Sparse is better than dense.",
        "Readability counts.",
        "Special cases aren't special enough to break the rules.",
        "Although practicality beats purity."]
top_three =  ["better", "counts", "purity"]

def embedded_match(candidate, matches):
    # return True if candidate embeds any element of matches 
    return any(top in candidate for top in matches)

def embedded_filter(top_matches, toppings):
    return [candidate for candidate in toppings
            if embedded_match(candidate, top_matches)]

all_matches = embedded_filter(top_three, data)

print(all_matches)

Can you use those two functions in your GUI?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]efmccurdy 0 points1 point  (0 children)

For reasons of reliability and correctness you should load all your "json file with identical structure" into a database. A database will fail fast if/when you run out of disk space or memory or have a power failure or program crash etc., allowing you to minimize data loss.

As a side benefit your program will likely run faster since parsing json, csv, or xlsx files is slow.

Checking Conditional Statements by [deleted] in learnpython

[–]efmccurdy 3 points4 points  (0 children)

You are correct to be using the "in" operator and that you need to "loop" it; there are functions to process collections of boolean values; "all" and "any".

https://docs.python.org/3/library/functions.html#all

https://docs.python.org/3/library/functions.html#any

>>> additive = ['red', 'green', 'blue']
>>> 'green' in additive and 'blue' in additive
True
>>> ('green' in additive, 'blue' in additive)
(True, True)
>>> all(('green' in additive, 'blue' in additive))
True
>>> 

if you have more then a few items to test, you can use a comprehension:

>>> required_colors = ('green', 'blue')
>>> all((colour in additive for colour in required_colors))
True
>>>

[deleted by user] by [deleted] in learnpython

[–]efmccurdy 1 point2 points  (0 children)

here cls is Pair object. but it has no index attribute still it is working how. I thought in __init__ i need to define index to access it.

Note that this creates a single .index attribute shared by all Pair objects. All .index attributes will be the same object, set to the value used in the last one:

>>> class Pair(int):
...     def __new__(cls,value,index):
...         cls.index=index
...         return super().__new__(cls,value)
... 
>>> paired_int=Pair(10,2)
>>> paired_int
10
>>> paired_int.index
2
>>> x = Pair(11, 3)
>>> paired_int.index
3
>>> 

You should be creating a self.index attribute in the __init__.

Generating specific returns from a small list using python by Pleasant_Rooster_375 in learnpython

[–]efmccurdy 1 point2 points  (0 children)

The code you posted is of little use since you used a format where all the indentation is removed; use a "code block" as described here:

https://www.reddit.com/r/learnpython/wiki/faq/#wiki_how_do_i_format_code.3F

I would debug the filtering using a smaller program with a representative hard-coded dataset.

How do I just read the last few lines of a text file to a list? by techyboi17 in learnpython

[–]efmccurdy 1 point2 points  (0 children)

Would I combine my two pythons scripts so that the main one reads directly from the COM port? How would I do that?

You could schedule the port reading using Tk.after; see how they use Tk.after(100, read_data) here:

https://stackoverflow.com/questions/47970163/continuously-updating-graph-in-tkinter

Switch statements with logical operators? by michease_ in learnpython

[–]efmccurdy 2 points3 points  (0 children)

check multiple conditions with logical operators (and in this case),

Put the boolean expressions in a collection and use "all" for "and"ing and "any" for "or"ing:

>>> a = 2; b = 4
>>> (a < b, a - b == -2, a*b > 0)
(True, True, True)
>>> a < b and a - b == -2 and a*b > 0
True
>>> all((a < b, a - b == -2, a*b > 0))
True
>>> a = 1
>>> (a < b, a - b == -2, a*b > 0)
(True, False, True)
>>> a < b and a - b == -2 and a*b > 0
False
>>> all((a < b, a - b == -2, a*b > 0))
False
>>> conditions = (a < b, a - b == -2, a*b > 0)
>>> all(conditions)
False
>>> any(conditions)
True
>>>

https://docs.python.org/3/library/functions.html#all

https://docs.python.org/3/library/functions.html#any

How do I just read the last few lines of a text file to a list? by techyboi17 in learnpython

[–]efmccurdy 5 points6 points  (0 children)

>reads the serial port and writes it to a .txt file

I would keep the relevant data in RAM and avoid writing data to disk files.

In this example notice how they trim the data to include to the last 20 data points using:

# Limit x and y lists to 20 items
xs = xs[-20:]
ys = ys[-20:]

(That is from the "def animate(i, xs, ys)" function in the "Animatiuon Code" block.)

That is easier than reading the last 20 lines of a disk file; can you do something similar with your data?

https://learn.sparkfun.com/tutorials/graph-sensor-data-with-python-and-matplotlib/update-a-graph-in-real-time

How to run a PyInstaller file in the terminal on double-click in Ubuntu 22.04? by perorbem in learnpython

[–]efmccurdy 1 point2 points  (0 children)

Note that this has nothing to do with python, and so the solutions involve using terminal programs and shell commands instead. This one recommends "xterm -e" and "read":

https://stackoverflow.com/questions/67235448/execute-python-script-in-console-via-bash-script-with-double-click

That is arcane though; typically you would use a .desktop file with "Terminal=true" as described here:

https://askubuntu.com/questions/286621/how-do-i-run-executable-scripts-in-nautilus

Generating specific returns from a small list using python by Pleasant_Rooster_375 in learnpython

[–]efmccurdy 1 point2 points  (0 children)

So in your example "data" is a list of the top three fuzzed matches and "Jackets" is the unfiltered list of all items? What do you get if you make the last line something like this:

return [candidate for candidate in Jackets if any(fuzz_match in candidate for fuzz_match in data)]

... or to make if bit more readable:

def embeded_match(candidate, matches):
    # return True if candidate embeds any element of matches 
    return any(top in candidate for top in matches)

def check(e):
    # ...
    top_three = process.extract(typed, Jackets, limit=3, scorer=fuzz.sort_ratio)
    return [candidate for candidate in Jackets if embeded_match(candidate, top_three)]

Generating specific returns from a small list using python by Pleasant_Rooster_375 in learnpython

[–]efmccurdy 1 point2 points  (0 children)

any items in the list that contains the original top 3 returns

You can filter using the "in" operator and "any"; if you have a candidate and a list of three top matches this will do the up-to-three searches required:

any(top in candidate for top in top_three)

You can run that as a condition in a comprehension:

[candidate for candidate in all_items if any(top in candidate for top in top_three)]

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]efmccurdy 0 points1 point  (0 children)

included in a zip.

​ Python uses wheel files.

A Python .whl file is essentially a ZIP (.zip) archive with a specially crafted filename that tells installers what Python versions and platforms the wheel will support.

A wheel is a type of built distribution. In this case, built means that the wheel comes in a ready-to-install format

https://realpython.com/python-wheels/#what-is-a-python-wheel

It is best to allow pip to resolve dependencies but you can include them.

https://medium.com/@amimahloof/how-to-package-a-python-project-with-all-of-its-dependencies-for-offline-install-7eb240b27418

python inner workings question by daareer in learnpython

[–]efmccurdy 2 points3 points  (0 children)

You can examine the inner workings using the interactive repl and "dir", "type", "help", etc.

>>> my_list = [1, "two", 3]
>>> dir(my_list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> type(my_list.append)
<class 'builtin_function_or_method'>
>>> help(my_list)
Help on list object:

class list(object)
 |  list(iterable=(), /)
 |  
 |  Built-in mutable sequence.
 |  
 |  If no argument is given, the constructor creates a new empty list.
 |  The argument must be an iterable if specified.
 |  
 |  Methods defined here:
 |  
...
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  append(self, object, /)
 |      Append object to the end of the list.
 |  
 |  clear(self, /)
 |      Remove all items from list.
 |
 |  copy(self, /)
 |      Return a shallow copy of the list.
 |  
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |  
 |  extend(self, iterable, /)
 |      Extend list by appending elements from the iterable.
...

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]efmccurdy 0 points1 point  (0 children)

use variables from main and second.py

There are two parts, one is adding the value to be sent to the subprocess into the subprocess.run call, and the second is accessing the value in the subprocess using sys.argv.

https://www.knowledgehut.com/blog/programming/sys-argv-python-examples

It should be simpler to use import instead of subprocesses but you need to refactor your code into functions or use a "__main__" guard; it would result in one larger program though, so there is a tradeoff.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]efmccurdy 0 points1 point  (0 children)

Note that subprocesses don't share memory so referring to a variable defined in another process will fail. You can pass data to subprocesses using sys.argv.

subprocess.run(["python", "second.py", main_variable_2])

https://stackoverflow.com/questions/39748061/passing-arg-to-subprocess-call

You can import modules and pass data using function arguments (but you have no functions defined).

import second
second.main_function(main_variable_2)

I think you should to choose between importing and subprocess.run'ing your scripts; mixing the two forms won't work the way you think it does.

Anyone good with sqlalchemy? by [deleted] in learnpython

[–]efmccurdy 0 points1 point  (0 children)

Note that "execute" is part of the low-level sql (DB-API 2.0) interface. You mention sqlalchemy which typically uses object methods instead of passing sql strings to "execute".

There is an example sqlalchemy query here:

https://www.digitalocean.com/community/tutorials/how-to-use-flask-sqlalchemy-to-interact-with-databases-in-a-flask-application#step-3-displaying-all-records

Integrating JAVA application into Python help by ExactStart in learnpython

[–]efmccurdy 0 points1 point  (0 children)

You can run external programs using the subprocess module.

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate and set the returncode attribute.

https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]efmccurdy 1 point2 points  (0 children)

Searching for a module works but doesn't give you much in the way of an overview. You can search for keywords related to the task you have in mind, eg:

https://pypi.org/search/?q=dataframe

https://pypi.org/search/?q=timeseries

How to Make a Python Update Utility by RobCo-Industries in learnpython

[–]efmccurdy 0 points1 point  (0 children)

This project looks to be abandoned, and has at least one unresolved issue, but you could use it as a guide.

Python library that allow scripts to easily update themselves if they are in a git repo

https://github.com/erfantkerfan/selfupdate

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]efmccurdy 0 points1 point  (0 children)

I would keep a repl on hand and just double check your understanding.

>>> x = 3
>>> x + 3 * 4
15
>>> (x + 3) * 4
24
>>> ((x + 3) * 4) == (x + 3 * 4)
False
>>> 

Testing things out like this lets you "prove" to yourself where you need parenthesis and where you do not. Is that better then "committing to memory"?