Python

3217 readers
2 users here now

News and discussions about the programming language Python


founded 5 years ago
MODERATORS
26
 
 

Meta is dedicating 3 engineers to get the nogil patches into cpython. There are some other companies stepping up as well. This is huge this is the closest we have ever been to solving the issue of the GIL.

27
 
 

Hello Python community! There are a lot of resources online targeted at beginners that want to learn Python but very rarely do you see articles talking about moving to Python when you already have tons of experience in other languages like Ruby, and especially, many years of Perl experience and is interested in moving to Python.

I'm not looking for information on how to program in Python, that's really easy to find and most of the learning curve will be learning about the standard libraries and overcoming the years of muscle memory from other languages. I'm looking for information on the following topics:

  • What's the recommended project structure for a library or a program that'll be distributed via PyPI?
  • What are the general best practices to follow when writing "clean Python code"?
  • What's the most commonly followed style guide for the language?
  • How does import work internally and how does it perform its path lookup for local files (specifically for importing modules internal to a project)?
  • How to properly set up pyenv for a project? (This one is tricky for me because the Python community loves pyenv and I'm used to having packages globally installed in Ruby and Perl)
28
29
16
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 
 

Hi,

Does Anyone have any great resources to learn python for a non-programmer? Youtube? Free online courses, etc?

Im starting out and there is a lot out there. They all look good too.

So far, i got python installed lol.

Thanks,

Edit: thanks everyone. I have some homework to do and check out all the resources !

30
 
 

Several years into Python, I am trying to get better about writing testable code and following the right coding conventions, and I am confused how to handle this situation. I would appreciate anybody who could lend their insight.

In my code, I have functions which depend on/reference global variables. These variables must be global. I want to write unit tests for these functions, and in order to fully test them, that means I must be able to manipulate those global variables.

Let's use this example code:

GLOBAL_DICT={'A':1}

def my_func()->None:
     GLOBAL_DICT[A]=GLOBAL_DICT[A]+1

I could, of course, write the function so that I can pass the global variable to it and that by default it will just add in the global variable. This makes it easier to write tests. But then PyCharm will complain that the argument is mutable and that it shadows the name from the outer scope. And if I do this, I need to define what happens if the variable is not passed (provide a default). I could set them to None, but then for doing type hinting I would have to do Union[dict,None] which makes the type hints look much more cluttered, and then pycharm will complain that I didn't handle a possible input of None in the function's code, but I shouldn't need to since I know it will never be None.

GLOBAL_DICT={'A':1}

def my_func(GLOBAL_DICT:dict=GLOBAL_DICT)->None:
     GLOBAL_DICT[A]=GLOBAL_DICT[A]+1

What is the best way to write these functions so they don't require passing in a bunch of globals every time I call and remain easy to write tests for?

31
32
33
 
 

Pyscan v0.1.4 | GitHub

Pyscan is the fastest CLI tool to find dependency vulnerabilities in your python projects.

  • blazingly fast scanner that can be used within large projects.
  • automatically finds requirements.txt, pyproject.toml or, the source code.
  • can be integrated into existing build processes.
  • In its early stage, thus hasn't been battle-hardened yet.

Install

pip install pyscan-rs

look out for the "-rs" part or

cargo install pyscan

Usage

Go to your python source directory (or wherever you keep your requirements.txt/pyproject.toml) and run:

> pyscan

or

> pyscan -d path/to/src

Pyscan is a tool written in Rust that uses OSV, which is an open source vulnerabilities database, which inspired me to make this tool.

34
35
 
 

I switched from notebook to labs recently and I'm missing how the notebook name is displayed in notebooks. it seems like the only way to know which notebook I'm in now is through the tab, but if I have multiple tabs open it compresses them.

Is there any extension or something that will display the notebook name (and make it easily editable) like in notebooks?

36
37
38
 
 

Let's say I have a context manager that provides a resource that then mutates on exit:

from contextlib import contextmanager

@contextmanager
def context():
    x = ['hi']
    yield x
    x[0] = 'there'

I found that if I want to make another context class that uses this, such that the context (before mutation) is valid, I have to pass it in:

class Example1:
    def __init__(self, obj):
        self.obj = obj
    def use_obj(self):
        print(self.obj)
    def __enter__(self):
        print("start")
        return self
    def __exit__(self, *exc):
        print("end")

with context() as x:
    with Example1(x) as y:
        y.use_obj()

prints:

start
['hi']
end

However, what I don't like is, let's say that obj is an internal detail of my class. I don't want the user to have to define it beforehand and pass it in.

The only way I can figure how to do this is by calling the context manager's __enter__() explicitly:

class Example2:
    def use_obj(self):
        print(self.obj)
    def __enter__(self):
        print("start")
        self.ctx = context()
        self.obj = self.ctx.__enter__()
        return self
    def __exit__(self, *exc):
        print("end")
        self.ctx.__exit__(None, None, None)

with Example2() as y:
    y.use_obj()

which also prints,

start
['hi']
end

For comparison, just as some other random attempt, the following doesn't work because the context ends when self.obj is created:

class Example3:
    def use_obj(self):
        print(self.obj)
    def __enter__(self):
        print("start")
        with context() as x:
            self.obj = x
        return self
    def __exit__(self, *exc):
        print("end")

with Example3() as y:
    y.use_obj()

which prints,

start
['there']
end

Okay, so my point is that Example2 is the right solution here. But, it's really ugly. So my question is, is there a better way to write Example2?

39
 
 

I'm setting up a new laptop and considering which of the (many) environment managers to use this time around. My standard has been miniconda, since a big plus for me is the ability to set and download specific python version for different projects all in one tool. I also quite like having global access to different environments (i.e. environments aren't tied to specific projects). I typically have a standard GenDataSci environment always available for initially testing things out, then if I know I'll be continuing as a single project I'll make a stand alone environment for it.

But I've also used poetry for tighter control and reproducibility when I'm actually packaging to publish on PyPI. Hatch looks interesting as well but I can't tell if it includes the ability to have separate python version installs for each environment.

What workflows and managers are people using now?

40
 
 

Brendan Metcalfe's intro series to list comprehensions is one of the best I've come across. In addition to showing how to use them, he compares it to other similar methods and shows why LCs can be more effective. Wanted to share his stuff here.

41
 
 

cross-posted from: https://programming.dev/post/21515

Some surprising, but valid, python syntax examples.

42
 
 

I’m looking for some conference talks about python that I could watch during some downtime at work. Give me your favorites!

43
 
 

I have a large object that I want to save to the disk because it takes a minute to generate. The OOM reaper kills the process while pickle.dump ing the object.

It's a tuple of dicts of tuple of array.array.

Can pickle dump in chunks? If not, is there another technique I can use?

44
45
46
 
 

Some neat tricks for using Makefiles with Python that I hadn't seen before. The one I have seen, having a help target, has been super useful in team environments and for debugging when used with variables.

47
48
49
50
 
 

(there is a discussion about this post here https://news.ycombinator.com/item?id=34787092 ...)

view more: ‹ prev next ›