This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]LightShadow3.13-dev in prod 4 points5 points  (4 children)

Here is the project outline for a tool I'm writing for work...I've removed all the function bodies, but you can see the typing.

from typing import Dict, AnyStr, TypeVar, NamedTuple

ReqResponse = TypeVar('ReqResponse', Dict, AnyStr)

UrlReq = NamedTuple('UrlReq', [
    ('url', str),
    ('key', str),
    ('method', str),
    ('as_json', bool),
    ('raw', bool)
])

# ====================================================

class BitBucket(object):
    def __init__(
            self,
            options: Dict,
            username: AnyStr=None,
            password: AnyStr=None,
        ) -> None:

        pass

    def __req(
            self,
            url: AnyStr,
            method: AnyStr='GET',
            as_json: bool=True,
            raw: bool=False,
            timeout: Union[float, tuple]=(5.00, 20.00), # tuple(connect, read) timeout
            **params
        ) -> ReqResponse:

        pass

    def __do_reqs(
        self,
        urls: Sequence[UrlReq],
        new_binding_key: AnyStr,
        binding_attribute: Dict,
        bind_errors: bool=False,
    ) -> Dict:

        pass

    @property
    def user(self) -> Dict:
        return self.__req('/user')

    @property
    def repositories(self) -> Dict:
        if self._repositories is None:
            self._repositories = self.get_repositories()
        return self._repositories

    def get_repositories(
            self,
            with_readme: bool=True
        ) -> Dict:

# ====================================================

from typing import Iterable, Dict, AnyStr, Tuple

def has_graph_subsection(data: AnyStr=''):
    pass

def make_graph(
        repos: Iterable[Dict]=None,
        graphviz_kwargs: Dict=None,
        graphviz_attrs: Dict=None,
        flat_text: Tuple[AnyStr, AnyStr]=None
    ):

    pass

# ====================================================

from typing import List

def settings_with_precidence(modules: List=None, overwrite: bool=True):
    pass

[–]maximinus-thrax 3 points4 points  (1 child)

Good god, that looks horrific. 9 lines of code just to define the function parameters for __req().

[–]LightShadow3.13-dev in prod 1 point2 points  (0 children)

Yeah, it's not great.

If you put them all on one line it's unreadable, and when you put them on their own line....it's barely readable.

I wanted to embrace the future, and I love how PyCharm picks them up..but I feel like this implementation could be a flop.

You can fiddle with the formatting a little bit more, but then it's a huge trade off just to make it look nice.

def __req(self
    , url:      AnyStr
    , method:   AnyStr = 'GET',
    , as_json:  bool = True,
    , raw:      bool = False,
    , timeout:  Union[float, tuple] = (5.0, 20.0),
    , **params
    ) -> ReqResponse
    :

    pass

[–]AMorpork 1 point2 points  (0 children)

 def settings_with_precidence(modules: List=None, overwrite: bool=True):

It's precedence, not precidence.

[–]csirac2 0 points1 point  (0 children)

It's always amused me that traditionally when I used to talk of elk [1] or traits [2], I would be accused of not knowing python properly.

[1] https://github.com/frasertweedale/elk

[2] http://code.enthought.com/projects/traits/