0

I'm pretty new to python, and currently playing with the zeroconf library.

when I try to register a service on the network, I'm seeing this in the function definition:

def register_service(self, info, ttl=_DNS_TTL):
    """Registers service information to the network with a default TTL
    of 60 seconds.  Zeroconf will then respond to requests for
    information for that service.  The name of the service may be
    changed if needed to make it unique on the network."""
    self.check_service(info)
    self.services[info.name.lower()] = info
    if info.type in self.servicetypes:
        self.servicetypes[info.type] += 1
    else:
        self.servicetypes[info.type] = 1
    now = current_time_millis()
    next_time = now
    i = 0
    while i < 3:
        if now < next_time:
            self.wait(next_time - now)
            now = current_time_millis()
            continue
        out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA)
        out.add_answer_at_time(DNSPointer(info.type, _TYPE_PTR,
                                          _CLASS_IN, ttl, info.name), 0)
        out.add_answer_at_time(DNSService(info.name, _TYPE_SRV,
                                          _CLASS_IN, ttl, info.priority, info.weight, info.port,
                                          info.server), 0)
        out.add_answer_at_time(DNSText(info.name, _TYPE_TXT, _CLASS_IN,
                                       ttl, info.text), 0)
        if info.address:
            out.add_answer_at_time(DNSAddress(info.server, _TYPE_A,
                                              _CLASS_IN, ttl, info.address), 0)
        self.send(out)
        i += 1
        next_time += _REGISTER_TIME

Anyone know what type info is meant to be?

EDIT
Thanks for providing the answer that it's a ServiceInfo class. Besides the fact that the docstring provides this answer when one goes searching for it. I'm still unclear on:

  • the process expert python programmers follow when encountering this sort of situation - what steps to take to find the data type for info say when docstring wasn't available?
  • how does python interpreter know info is of ServiceInfo class when we don't specify the class type as part of the input param for register_service? How does it know info.type is a valid property, and say info.my_property isn't?
snowbound
  • 1,692
  • 2
  • 21
  • 29
  • Where are you seeing this definition? I can't seem to find `register_service` in https://github.com/wmcbrine/pyzeroconf – ryachza Aug 22 '15 at 11:52
  • I was going to add `info.__class__` to the `register_service` function, but I can't call the function as `info` is an input parameter. Any other way I could call the function? If I added `info=None` would it invalidate the data type? – snowbound Aug 22 '15 at 11:52
  • oops, sorry @ZJM. Wrong link. Here's the correct one: https://github.com/jstasiak/python-zeroconf – snowbound Aug 22 '15 at 11:54
  • 2
    One thing that hasn't explicitly been pointed: All codes needs unit tests, and code in dynamic languages need it even more. So if you are not sure what data type to pass in, have a look at what type the test code passes in. If there is neither tests nor documentation, don't use that particular library. It's broken. – Sven Marnach Aug 23 '15 at 10:24

2 Answers2

2

It is an instance of ServiceInfo class.

It can be deduced from reading the code and docstrings. register_service invokes check_service function which, I quote, "checks the network for a unique service name, modifying the ServiceInfo passed in if it is not unique".

Konstantin
  • 24,271
  • 5
  • 48
  • 65
  • hi @Alik. Which docstrings are you seeing this in? I searched through all instance of `info` in the `zeroconf.py` module, and couldn't find it properly defined. Only see it used with . (dot) notation e.g. `info.type`, `info.address` – snowbound Aug 22 '15 at 11:57
  • 2
    @snowbound: Welcome to the world of dynamic languages! What you should take home from this is to write good docstrings in your own code, documenting data types where it's not obvious. :) – Sven Marnach Aug 22 '15 at 12:00
  • hi @SvenMarnach and Alik, I've updated my original question to call out what I'm not clear on (sorry, my background is strongly typed / compile languages) – snowbound Aug 23 '15 at 01:27
  • @snowbound questions in your edit require a lengthy answer. I recommend you to task them as separate questions. – Konstantin Aug 23 '15 at 05:24
  • @snowbound See https://docs.python.org/2/reference/datamodel.html#the-standard-type-hierarchy (a paragraph that starts with 'Both class types (new-style classes) and class objects ..') to understand how python knows `info.type` is a valid property, and `info.my_property` isn't. – Konstantin Aug 23 '15 at 05:26
  • @snowbound also there is a special attribute https://docs.python.org/2/library/stdtypes.html#instance.__class__ – Konstantin Aug 23 '15 at 06:50
1

It looks like it should be a ServiceInfo. Found in the examples of the repository:

https://github.com/jstasiak/python-zeroconf/blob/master/examples/registration.py

Edit

  1. I'm not really sure what to say besides "any way I have to". In practice I can't really remember a time when the contract of the interface wasn't made perfectly clear, because that's just part of using Python. Documentation is more a requirement for this reason.
  2. The short answer is, "it doesn't". Python uses the concept of "duck typing" in which any object that supports the necessary operations of the contract is valid. You could have given it any value that has all the properties the code uses and it wouldn't know the difference. So, per part 1, worst case you just have to trace every use of the object back as far as it is passed around and provide an object that meets all the requirements, and if you miss a piece, you'll get a runtime error for any code path that uses it.

My preference is for static typing as well. Largely I think documentation and unit tests just become "harder requirements" when working with dynamic typing since the compiler can't do any of that work for you.

ryachza
  • 4,460
  • 18
  • 28
  • hi @ryachza, I've updated my original question to call out what I'm not clear on (sorry, my background is strongly typed / compile languages) – snowbound Aug 23 '15 at 01:28