651
-11

Are you doing data science? Statistics? No?

Then for god's sake don't use pandas, you just look dumb af when you pull several MB of a package just to load csv. If you find yourself doing that, just stop programming and look for another job

Thanks for attention

652
19
653
20
submitted 3 years ago by travis@lemmy.blue to c/python@programming.dev

Hi everyone,

I'm pleasantly surprised and very thankful for the traction Plemmy has received in a few short weeks since its initial release!

The primary goal of Plemmy is simple: offer access to the LemmyHttp API in Python, allowing users to interact with any Lemmy instance using Python.

Plemmy's LemmyHttp object does just this, returning Python request.Response objects resulting from Lemmy API calls. All LemmyHttp functions have been implemented!

With release 0.3.0, Plemmy now offers a way to parse request.Response objects, extracting all information and placing them in easy-to-use Python objects. The design of these functions/objects closely mirrors the objects and data types defined in the lemmy-js-client.

These additions should make interacting with Lemmy in Python easier than ever. Check out Plemmy's repository for example usage (more documentation to come!).

Thanks for the continued support,

Travis

654
7

Featuring:

  • Identifying bundled libraries on Python distributions to help scanning for vulnerabilities.
  • Trusted Publisher adoption metrics.
  • GitHub push protection for PyPI API tokens.

Lots of great stuff!

655
12

I am writing an object-oriented app to help our developers manage some cloud systems. I'd like to make the configuration information available to all the classes, but I'm not sure of a good way to do that. Everything I can think of seems to fall under the category of "global variables" which as far as I know is a Very Bad Thing.

I already have a logging Mixin class that enables logging for every class that inherits it, and I was wondering if that's the right way to approach the configuration data:

class LoggingMixin:
    @classmethod
    @property
    def log(cls):
        return logging.getLogger(cls.__name__)

class TestClassA(LoggingMixin):
    def testmethod1(self):
        self.log.debug("debug message from test class A")

if __name__ == "__main__":
    logging.basicConfig(
        format="{created:<f} {levelname:>5s} {name}.{funcName:>8s} |{message}|",
        level=logging.DEBUG,
        style="{",
    )

    a = TestClassA()
    a.testmethod1()

Outputs (in case you are curious)

1688494741.449282 DEBUG TestClassA.testmethod1 |debug message from test class A|

What's a good way of making data from a class available to all classes/objects? It wouldn't be static, it'd be combined from a JSON file and any command line parameters.

If I copied the example above but changed it to a ConfigMixin, would that work? With the logging example, each class creates its own logger object when it first calls self.log.debug, so that might not work because each object needs to get the same config data.

Is there a pattern or other design that could help? How do you make configuration data available to your whole app? Do you have a config object that can get/set values and saves to disk, etc?

Thank you for reading, my apologies for poorly worded questions.

656
14

An exploration of partial() and partialmethod() in functools

657
37
submitted 3 years ago* (last edited 3 years ago) by lig@social.fossware.space to c/python@programming.dev

VS Code is dropping Python 3.7 support in September 2023 and with the following Python releases they are planning to drop support of a Python version in the first extension release of the year following after EOL.

658
4

Once you've installed it, pytest will produce nice colourised diffs for any assert == :

😍

Via https://mastodon.social/@hynek/110479665200902390

659
11
submitted 3 years ago* (last edited 3 years ago) by InternetPirate@lemmy.fmhy.ml to c/python@programming.dev

Last month, I developed a script because lemmy.ml had become too slow. Unfortunately, I have the same problem again, but this time there are too many instances to evaluate, causing the script to take an excessively long time to complete. I'm seeking advice on how to enhance the script to simultaneously ping multiple instances. Are there any alternative scripts available that might provide a more efficient solution?

git clone https://github.com/LemmyNet/lemmy-stats-crawler
cd lemmy-stats-crawler
cargo run -- --json > stats.json
#!/usr/bin/env python3
import json
import time
import requests
import requests.exceptions

from typing import List, Dict

TIME_BETWEEN_REQUESTS = 5  # 10 * 60 = 10 minutes
TIME_TOTAL = 60  # 8 * 60 * 60 = 8 hours


def get_latency(domain):
    try:
        start = time.time()
        if not domain.startswith(("http://", "https://")):
            domain = "https://" + domain
        requests.get(domain, timeout=3)
        end = time.time()
        return end - start
    except requests.exceptions.Timeout:
        return float("inf")


def measure_latencies(domains, duration):
    latencies = {}
    start_time = time.time()
    end_time = start_time + duration
    while time.time() < end_time:
        latencies = measure_latencies_for_domains(domains, latencies)
        time.sleep(TIME_BETWEEN_REQUESTS)
    return latencies


def measure_latencies_for_domains(domains, latencies):
    for domain in domains:
        latency = get_latency(domain)
        latencies = add_latency_to_domain(domain, latency, latencies)
    return latencies


def add_latency_to_domain(domain, latency, latencies):
    if domain not in latencies:
        latencies[domain] = []
    latencies[domain].append(latency)
    return latencies


def average_latencies(latencies):
    averages = []
    for domain, latency_list in latencies.items():
        avg_latency = sum(latency_list) / len(latency_list)
        averages.append((domain, avg_latency))
    return averages


def sort_latencies(averages):
    return sorted(averages, key=lambda x: x[1])


def get_latency_report(domains, duration):
    latencies = measure_latencies(domains, duration)
    averages = average_latencies(latencies)
    return sort_latencies(averages)


def get_instances(data: Dict) -> List[Dict]:
    instances = []
    for instance_details in data["instance_details"]:
        instances.append(instance_details)
    return instances


def get_domains(instances: List[Dict]) -> List[str]:
    return [instance["domain"] for instance in instances]


def load_json_data(filepath: str) -> Dict:
    with open(filepath) as json_data:
        return json.load(json_data)


def main():
    data = load_json_data('stats.json')
    instances = get_instances(data)
    domains = get_domains(instances)
    report = get_latency_report(domains, TIME_TOTAL)
    for domain, avg_latency in report:
        print(f"{domain}: {avg_latency:.2f} seconds")


if __name__ == "__main__":
    main()
660
9
submitted 3 years ago* (last edited 3 years ago) by wasabi@feddit.de to c/python@programming.dev

After learning about TYPE_CHECKING i made it a habit to put all imports that were only needed for type checking into an if TYPE_CHECKING: guard. But now I am wondering if that is actually intended to be used like that. Checking whether an import is only needed at type checking time can get quite tedious and sometimes you run into situations were you introduced some code that made the import a requirement at runtime.

How do you use TYPE_CHECKING? Whenever it is possible or only when using it actually solves a circular import?

661
6

This usage test works already. Whadda ya guys think?

662
2

The Pure #Python #webapp survey is here - https://metaperl.github.io/pure-python-web-development/intro.html

Recent Updates

663
4

pytheus is a modern python library for collecting prometheus metrics built with multiprocessing in mind.

664
13
665
5
666
20
submitted 3 years ago* (last edited 3 years ago) by mhz@lemmy.world to c/python@programming.dev

Hello fellow lemmians (I hope that is correct), I want to learn a programming language in my free time, like a hobby and not necessarly for work. I think a general purpose language like python would be a good choice. I'm the type who prefer paperback books to minimize distraction and I would like your recommendation for a book, Im thinking about "Python crash course 3d edition" but feel free to suggest something else

667
20

Simple Python [executable] version management, that is.

668
11
submitted 3 years ago* (last edited 3 years ago) by jnovinger@programming.dev to c/python@programming.dev

Via https://mastodon.social/@hynek/110655873748943682

It has a single purpose: to help you write correct retry logic effortlessly.

If you’ve looked at it before: it now has proper docs, support for async, and retries of arbitrary code blocks.

669
18
670
23
Pillow 10.0.0 release (pillow.readthedocs.io)
671
11
submitted 3 years ago* (last edited 3 years ago) by jnovinger@programming.dev to c/python@programming.dev

The Github profile: https://github.com/davep/

672
13
673
9
674
7

Hi everyone!
I am learning about using selenium and/or playwright and I'm struggling with something that I am struggling to find a good solution to.
Prevent webRTC leaking my real IP when using proxies with either Selenium or Playwright.

There's plenty of janky workarounds I've seen but they all just seem like they have issues and/or cause issues. What would be the right way to prevent WebRTC IP leaking?

675
88
submitted 3 years ago by Ac5000@lemmy.ml to c/python@programming.dev

After seeing the work someone else did with migrating your subscriptions across accounts. I took their code and expanded upon it to fully synchronize your Lemmy instance accounts.

Feel free to give it a try. It's been useful for me to have accounts in multiple instances with the instability problems right now from the big influx of users.

Let me know what you think. Hopefully it can help a few people.

view more: ‹ prev next ›

Python

8022 readers
1 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 3 years ago
MODERATORS