Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
PSA - Malicious software libraries in the official Python package repository (xpost /r/netsec)
http://www.nbu.gov.sk/skcsirt-sa-20170909-pypi/

/r/Python
https://redd.it/709vch
Anyone have a high res version of the XKCD Python Antigravity Image?

Randall Munroe is giving a talk here in a couple of weeks and I'd like to get him to sign a copy.

https://xkcd.com/353/

I can't see it available to buy in the XKCD store. Can anyone please link me to a high res version?



/r/Python
https://redd.it/70a9n8
Getting users to update or create

I seem to have got myself confused this morning, like one of those days you begin to question how to spell a simple word you should know well. Anyway...

What's the best way to handle users eg updating an address vs creating a new one?
This current app will have lots of other models tied to the user's address (services etc....), we want to be able to let them update their current address (eg fill in missing details or correct a mistake) or create a new address eg when they move house (or add an additional property).

From a users point of view they shouldn't have to care whether they are updating their current address or creating a new one (moving house) they just want to change their address.
What's the best way to handle this?
Debating if it should just be a single form which in the view uses create_or_update and then checks whatever is the unique property of the address?

Additionally what's the best way to have a unique property on an address?
I originally had `unique_together(house_name_or_number, postcode)` but it's better to have separate fields for `house_name` and `house_number`but now not sure how to create a unique identifier now they are separate? (eg to prevent adding properties that already exist).

/r/django
https://redd.it/708m2q
Learning jquery/ajax to develop frontend over django - advice from full-stack developers?

Hey guys,
I've been learning django for 2 months now, and learnt a crapload already. I'm developing my webapp while learning, and now I'd like to learn enough jquery/ajax to implement some basic stuff like better looking user forms, save buttons and opening pages on modals (I think that's how it's called).
But honestly, I'm intimidated by jquery, mainly by ajax, from what I've dug into so far. And there doesn't seem to be such a linear way to learn things like with django. Or it's because I'm starting.

I like to skip steps and go straight to the action, which sometimes is fine and sometimes makes things harder. My question is: should I learn jquery in a regular fashion, and if yes, from which resources?



/r/django
https://redd.it/70dhzc
[AF]Setting up a version controlled build environment for several developers to access/build locally/push to live

Title mostly says it all, I want to set up a version controlled directory in which my flask app lives. I then want to be able to have all my developers use svn or git (my version control of choice still up in the air) to be able to sync that directory onto any machine they feel like working on. Within this app I want to have some kind of "build environment" (that's what I think it's called at least). At the end of the day I need something similar to Make like how I've seen 'make clean' to remove any artifacts 'make dev' to spin up a local webserver of the app and install the needed virtual env with required packages and finally a 'make install' that pushes it out to the production server and restarts apache.

Obviously make can do this, however after talking with an older coworker who lives and breathes by make, he had mentioned that it was totally overkill for what I am trying to do and that there are other more elegant solutions designed specifically for this process.

I have been searching over the past week to try and find some information about standard practices and basically what my options are to do this, but I can't find much of anything useful and I think it's because the term "build environment" may not be exactly the search I need.

If anyone can help point me in the right direction I would be greatly appreciative!

/r/flask
https://redd.it/70a95d
Its ok if I just replace SQLite settings with my Postgresql DB in a blank project?

Hi, Im struggling with this, the thing is everytime you create a Django project the settings by default works with SQLite. I have Postgresql locally and I just want to avoid using SQLite.

What I do is remove the SQLite settings by default and replace them with the ones I have with my local DB in Postgresql, but its so hard to setup my website on Heroku after doing this since it doesnt show up after typing heroku open.

 

I set everything else fine, I mean, I might be missing something of course but I avoid to setup few things that you usually do if you want SQLite for a local DB and Postgres in Server. There is no documentation about removing SQLite completely, what I see in many many tutorials and other sites, its that SQLite should be used for your local DB which is the thing I want to avoid and use Postgresql instead, locally and in a server(heroku). Seriously, its hilarious that there is not a single post about creating a django project and get rid of SQLite, I actually might do a post about it since nobody has done this before it seems lol.
Any tips? Sorry, Im kinda new to this, but really want to get rid of this issues to fully start working with Django.

/r/django
https://redd.it/70e3az
Manipulating single bit values in python for compression?

Hi.

I have been reading about compression and decided to implement the Huffman coding for simple strings.

I have been able to write a program which does implement the algorithm by generating Huffman codes using strings of 0s and 1s. However, the problem with this is that this doesn't actually result in any compression as I am just representing Huffman codes as strings of 8 bit characters and not as the n-bit values they represent eg '010' is 8*3 bits in size instead of 3 bits (010) in size I would like.

My question is, is there a way to manipulate integers in python so I am working with just raw 1s and 0s instead of having to manipulate values as integers are represented this way in python? Or as python has arbitrary integer length, is this automatically done? Or does python just not allow this level of bit manipulation?

Any help is appreciated

/r/Python
https://redd.it/70hb4o
List comprehension help

Can someone recommend a good blog post or videos to learn this concept. Thank you

/r/Python
https://redd.it/70hybi
[AF] Flask-Bootstrap + WTForms Layout Issue

Having some trouble with the layout of customized SelectMultipleField.

Here is what I'm seeing:
[Checkbox Form](https://image.ibb.co/bCJwZQ/Screen_Shot_2017_09_16_at_7_42_32_AM.png)

Need to figure out how to:

-Have the checkboxes not run into other form fields

-Remove the box outline from checkbox field


This seems to be a common way to create checkboxes in WTForms, but I haven't been able to find anyone having the same problem. Not sure if I should be looking to modify my code, the Bootstrap CSS files, or something else.

Here is the form code:

class CheckBox(SelectMultipleField):
widget=widgets.ListWidget(prefix_label=True)
option_widget=widgets.CheckboxInput()

class NumberForm(Form):
data = CheckBox(label='Choose an option', choices=data_choices)
email = TextField('What is your email?', validators=[Required()])
submit = SubmitField('Submit')

---

Here is how the form is called in the HTML template:

{% import "bootstrap/wtf.html" as wtf %}
...
{{ wtf.quick_form(form) }}

---

Can provide more detail if necessary, but wanted to keep it simple. Any help is much appreciated. Thanks



/r/flask
https://redd.it/70hbqy
Flask-APScheduler, does anyone have experience with using this? I keep running into an error.

Bascially, I start my app the first time, it runs, then I shut it down and start it again and I get a error:

apscheduler.jobstores.base.ConflictingIdError: 'Job identifier (job1) conflicts with an existing job'

It has to do with APscheduler trying to create the same jobid every time I run the app, but I am not sure how to fix this error.


Link to [SO](https://stackoverflow.com/questions/46242710/flask-apscheduler-conflicting-job-error) post.

/r/flask
https://redd.it/70byje
Django model design help

Hello,

I have a web app which I'm planning to monetize soon and I'm looking for a little guidance setting up the models. The payment system is Stripe. My current models are as follows:

**UserProfile (truncated for brevity):**


class UserProfile(models.Model):

yrs_experience = (
#Choices...
)

tradingstyles = (
#Choices...
)

analysisstyles = (
#Choices...
)

instruments = (
#Choices...
)

user = models.OneToOneField(User, on_delete=models.CASCADE)
location = models.CharField(max_length=30, blank=True)
birthdate = models.DateField(null=True, blank=True)
trading_style = models.CharField(max_length=30, choices=tradingstyles, default='NS')
analysis_style = models.CharField(max_length=30, choices=analysisstyles, default='NS')
instrument = models.CharField(max_length=30, choices=instruments, default='NS')
experience = models.CharField(max_length=10, choices=yrs_experience, default='NS')


def __str__(self): # __unicode__ for Python 2
return self.user.username


**PlanProfile (truncated for brevity):**

class PlanProfile(models.Model):

plans = (
#Choices...
)

user = models.OneToOneField(User, on_delete=models.CASCADE)
stripe_id = models.CharField(max_length=30, default='NA')
sub_id = models.CharField(max_length=30, default='NA')
is_premium = models.BooleanField(default=False)
plan_type = models.CharField(max_length=15, choices=plans, default='FREE')

def __str__(self): # __unicode__ for Python 2
return self.user.username

Currently when a customer signs up for a paid plan, the PlanProfile is updated to reflect the customer ID and subscription ID returned from Stripe, as well as setting the premium flag to true and setting their plan type so that I can manage access control within the views (via a few custom decorators). When a customer wishes to change plans, I currently have it set up so that they have to cancel their current one and then sign up for the new one. This works fine but on the Stripe end I'm executing it by deleting the subscription and the customer and setting the IDs in my DB back to 'NA' so when they sign up again it just makes a new call to Stripe and makes a new customer and subscription and then updates the DB again with the new IDs. Again that works great BUT when I delete the customer I lose their payment history, old invoices, old charges, old subscription history, etc. So, I'd like to get around that by adding a couple models to my own database and also I plan to implement an upgrade/downgrade system with Stripe as well as an alternative to cancelling/rejoining. Either way I'd like to keep the customer's payment history in my own database just for completeness so I'm looking for a little guidance with how I should build the additional models. The components I am guessing I'll want to keep from Stripe are:

- Invoices (which contain a customer ID, subscription ID, charge ID and invoice ID)
- Charges (I just want the charge ID so I have a record of past charges I guess)
- Subscriptions/subscription history for each customer(?) (Each one has an ID, and I can either change plans within the same ID or delete/recreate the subscription with a new ID/new plan in Stripe)
- Refunds (Issued if a customer cancels the sub before expiration, and is identified by a refund ID)

**Constraints:**

- Each customer can only have one subscription at a time
- Each charge is unique to a specific invoice
- An invoice may (not sure?) have multiple charges
- A subscription ID is unique to only one customer
- A charge is unique to a specific customer
- A refund is unique to a specific charge
- Possibly others I'm not thinking of...

So, I'm not a database designer and I'm not sure if/how I should design this. I'm trying to figure out which tables/models I need, e.g. a subscriptions table, a charges table, an invoices table, etc. I most likely don't need them all and would like to keep it as simple as possible. So, any insight on how you would do it?

Thanks!

/r/django
https://redd.it/70h6bo
Render just one MultiValueField field?

Is it possible to render only one specific MultiValueField field in Django 1.10.5?


/r/django
https://redd.it/70h4hk