Skip to content

Commit

Permalink
Update code for python>=3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-shtaub committed Jan 19, 2022
1 parent f1c37be commit c81e2c9
Show file tree
Hide file tree
Showing 37 changed files with 101 additions and 163 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog
0.6.0 (unreleased)
------------------

- Nothing changed yet.
- Update code for python>=3.7


0.5.0 (2018-08-30)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 People Doc
Copyright (c) 2022 People Doc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 0 additions & 2 deletions demo/demo/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-03-01 12:59
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
Expand Down
2 changes: 1 addition & 1 deletion populous/backends/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contextlib


class Backend(object):
class Backend:

def __init__(self, *args, **kwargs):
self.closed = False
Expand Down
17 changes: 6 additions & 11 deletions populous/backends/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
import contextlib
import uuid

try:
from functools import lru_cache
except ImportError:
from functools32 import lru_cache
from functools import lru_cache

from populous.compat import range
from populous.exceptions import BackendError
from .base import Backend

Expand All @@ -22,7 +18,7 @@

class Postgres(Backend):
def __init__(self, *args, **kwargs):
super(Postgres, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

self._current_cursor = None

Expand Down Expand Up @@ -89,7 +85,7 @@ def count(self, table, where=None):
with self.cursor as cursor:
cursor.execute("SELECT count(*) FROM {table} {where}".format(
table=table,
where="WHERE ({})".format(where) if where else '',
where=f"WHERE ({where})" if where else '',
))
return cursor.fetchone()[0]

Expand All @@ -104,8 +100,8 @@ def select_random(self, table, fields=None, where=None, max_rows=1000):
table=table,
fields=', '.join(fields),
proportion=min(max_rows / float(count), 1.0),
extra_where="AND ({})".format(where) if where else '',
limit="LIMIT {}".format(max_rows)
extra_where=f"AND ({where})" if where else '',
limit=f"LIMIT {max_rows}"
)
)

Expand All @@ -123,8 +119,7 @@ def select(self, table, fields):
)
)

for value in cursor:
yield value
yield from cursor

@lru_cache()
def get_pk_column(self, table):
Expand Down
2 changes: 1 addition & 1 deletion populous/bloom.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import peloton_bloomfilters as bloomfilter


class BloomFilter(object):
class BloomFilter:

def __init__(self, capacity=1000, error_rate=0.000001):
self._capacity = capacity
Expand Down
7 changes: 3 additions & 4 deletions populous/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

from populous.bloom import BloomFilter
from populous.buffer import Buffer
from populous.compat import basestring
from populous.exceptions import ValidationError
from populous.item import Item, COUNT_KEYS, ITEM_KEYS


logger = logging.getLogger('populous')


class Blueprint(object):
class Blueprint:

def __init__(self, items=None, vars_=None, backend=None):
self.items = OrderedDict(items or {})
Expand Down Expand Up @@ -56,7 +55,7 @@ def add_item(self, description):
parent_name = description['parent']
if parent_name not in self.items:
raise ValidationError(
"Parent '{}' does not exist.".format(parent_name)
f"Parent '{parent_name}' does not exist."
)
parent = self.items[parent_name]
if not name:
Expand Down Expand Up @@ -97,7 +96,7 @@ def add_item(self, description):
pass
elif isinstance(count, int):
item.add_count(number=count)
elif isinstance(count, basestring) and count[0] == '$':
elif isinstance(count, str) and count[0] == '$':
item.add_count(number=count)
else:
if not isinstance(count, dict):
Expand Down
2 changes: 1 addition & 1 deletion populous/buffer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import deque, OrderedDict


class Buffer(object):
class Buffer:

def __init__(self, blueprint, maxlen=1000):
self.blueprint = blueprint
Expand Down
7 changes: 3 additions & 4 deletions populous/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import click
import click_log
import six

from .loader import load_blueprint
from .exceptions import ValidationError, YAMLError, BackendError
Expand All @@ -16,7 +15,7 @@ def get_blueprint(files, **kwargs):
try:
return load_blueprint(*files, **kwargs)
except (YAMLError, ValidationError) as e:
raise click.ClickException(six.text_type(e))
raise click.ClickException(str(e))
except Exception as e:
raise click.ClickException("Unexpected error during the blueprint "
"loading: {}".format(e))
Expand Down Expand Up @@ -60,7 +59,7 @@ def _generic_run(modulename, classname, files, **kwargs):
logger.info("Have fun!")

except BackendError as e:
raise click.ClickException(six.text_type(e))
raise click.ClickException(str(e))


@run.command()
Expand Down Expand Up @@ -92,6 +91,6 @@ def generators():
doc = (generator.__doc__ or '').strip()

if doc:
click.echo("{} - {}".format(name, doc))
click.echo(f"{name} - {doc}")
else:
click.echo(name)
25 changes: 5 additions & 20 deletions populous/compat.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
import sys

from builtins import range
from builtins import zip
from six import string_types as basestring


PY2 = sys.version_info[0] == 2


if PY2:
from string import letters as ascii_letters
else:
from string import ascii_letters

try:
from functools import cached_property
except ImportError:
from cached_property import cached_property

__all__ = [
'ascii_letters',
'basestring',
'PY2',
'range',
'zip',
'cached_property',
]
2 changes: 0 additions & 2 deletions populous/django/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import absolute_import

from django.apps import AppConfig


Expand Down
8 changes: 3 additions & 5 deletions populous/django/management/commands/populous.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from __future__ import absolute_import

import argparse
from collections import OrderedDict
from functools import partial

import yaml

from django.apps import apps
from django.core import validators
from django.core.management.base import BaseCommand
from django.db import models

import yaml
from cached_property import cached_property

from populous.compat import cached_property

class SkipField(Exception):
pass
Expand Down
19 changes: 6 additions & 13 deletions populous/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
from .compat import PY2


class PopulousError(Exception):

if PY2:
def __unicode__(self):
return str(self).decode('utf-8')

pass

class YAMLError(PopulousError):

def __init__(self, filename, problem):
super(YAMLError, self).__init__(
"Error parsing '{}': {}".format(filename, problem)
super().__init__(
f"Error parsing '{filename}': {problem}"
)


class ValidationError(PopulousError):

def __init__(self, *args, **kwargs):
super(ValidationError, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.filename = kwargs.pop('filename', None)

def __str__(self):
msg = super(ValidationError, self).__str__()
msg = super().__str__()
if self.filename:
return "File '{}': {}".format(self.filename, msg)
return f"File '{self.filename}': {msg}"
else:
return msg

Expand Down
6 changes: 2 additions & 4 deletions populous/factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@


class ItemFactory(object):
class ItemFactory:

def __init__(self, item, parent=None):
self.item = item
Expand All @@ -12,7 +10,7 @@ def __init__(self, item, parent=None):

def __getattribute__(self, name):
try:
return super(ItemFactory, self).__getattribute__(name)
return super().__getattribute__(name)
except AttributeError:
generated = self._generated

Expand Down
25 changes: 13 additions & 12 deletions populous/generators/base.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import random

from cached_property import cached_property
from faker import Factory

from populous.bloom import BloomFilter
from populous.compat import cached_property
from populous.exceptions import GenerationError
from populous.exceptions import ValidationError
from populous.vars import Expression, parse_vars
from populous.bloom import BloomFilter
from populous.vars import Expression
from populous.vars import parse_vars

fake = Factory.create()


class BaseGenerator(object):
class BaseGenerator:

def __init__(self, item, field_name, **kwargs):
self.item = item
Expand Down Expand Up @@ -62,10 +63,10 @@ def parse_vars(self, value):
return parse_vars(value)


class NullableMixin(object):
class NullableMixin:

def get_arguments(self, nullable=0, **kwargs):
super(NullableMixin, self).get_arguments(**kwargs)
super().get_arguments(**kwargs)

if not nullable:
self.nullable = 0
Expand All @@ -77,22 +78,22 @@ def get_arguments(self, nullable=0, **kwargs):
def get_generator(self):
if self.nullable:
return self.generate_with_null()
return super(NullableMixin, self).get_generator()
return super().get_generator()

def generate_with_null(self):
generator = super(NullableMixin, self).get_generator()
generator = super().get_generator()
while True:
if random.random() <= self.evaluate(self.nullable):
yield None
else:
yield next(generator)


class UniquenessMixin(object):
class UniquenessMixin:
MAX_TRIES = 10000

def get_arguments(self, unique=False, **kwargs):
super(UniquenessMixin, self).get_arguments(**kwargs)
super().get_arguments(**kwargs)

self.unique = bool(unique)
if isinstance(unique, (list, tuple)):
Expand All @@ -109,13 +110,13 @@ def get_arguments(self, unique=False, **kwargs):
def get_generator(self):
if self.unique:
return self.generate_uniquely()
return super(UniquenessMixin, self).get_generator()
return super().get_generator()

def generate_uniquely(self):
seen = self.seen
unique_with = self.unique_with
tries = 0
for value in super(UniquenessMixin, self).get_generator():
for value in super().get_generator():
if isinstance(value, tuple) and hasattr(value, 'id'):
key = value.id
else:
Expand Down
2 changes: 1 addition & 1 deletion populous/generators/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class Boolean(Generator):

def get_arguments(self, ratio=0.5, **kwargs):
super(Boolean, self).get_arguments(**kwargs)
super().get_arguments(**kwargs)

self.ratio = ratio

Expand Down
2 changes: 1 addition & 1 deletion populous/generators/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Choices(Generator):

def get_arguments(self, choices=(), **kwargs):
super(Choices, self).get_arguments(**kwargs)
super().get_arguments(**kwargs)

if isinstance(choices, str):
self.from_var = True
Expand Down
4 changes: 2 additions & 2 deletions populous/generators/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DateTime(Generator):

def get_arguments(self, past=True, future=False, after=None, before=None,
**kwargs):
super(DateTime, self).get_arguments(**kwargs)
super().get_arguments(**kwargs)

self.past = past
self.future = future
Expand Down Expand Up @@ -68,5 +68,5 @@ def generate(self):
class Date(DateTime):

def generate(self):
for dt in super(Date, self).generate():
for dt in super().generate():
yield dt.date()
Loading

0 comments on commit c81e2c9

Please sign in to comment.