GithubHelp home page GithubHelp logo

date and datetime support about typedload HOT 7 CLOSED

ltworf avatar ltworf commented on September 1, 2024
date and datetime support

from typedload.

Comments (7)

ltworf avatar ltworf commented on September 1, 2024 1

It is not implemented, I don't know if I will ever implement it, but there are handlers to do this sort of things.

dumper = typedload.datadumper.Dumper() # Create a Dumper object, so we can modify it

# Let's add a new type handler to the object, so it can handle one more type
dumper.handlers.append(
    (
        lambda x: isinstance(x, datetime.date), # Function that matches the type
        lambda self, value: repr(value), # Function that does something with the value
    )
)

print(
    dumper.dump( # Dump using the custom Dumper object
        Point(x=5.0, y=6.0, created_at=date(2019, 1, 1))
    )
)

I used repr() to do the dump, but you can replace it with whatever you like.

Be warned that loading this thing back might be complicated. Do you need to do that?

from typedload.

ltworf avatar ltworf commented on September 1, 2024

Dumping the date object as a date object rather than converting it to string would work as well I guess.

dumper = typedload.datadumper.Dumper() # Create a Dumper object, so we can modify it
dumper.basictypes.add(datetime.date) # Add datetime.date to the set of types that do no action.

print(
    dumper.dump( # Dump using the custom Dumper object
        Point(x=5.0, y=6.0, created_at=date(2019, 1, 1))
    )
)

# Now it is trivial to load
loader=typedload.dataloader.Loader()
loader.basictypes.add(datetime.date)
loader.load(Point(x=5.0, y=6.0, created_at=date(2019, 1, 1)), Point)

The disadvantage of this method is that you can't serialize to json. Which is why the type isn't added by default in the 1st place.

from typedload.

marlonjan avatar marlonjan commented on September 1, 2024

Thanks for your quick reply, @ltworf!

Be warned that loading this thing back might be complicated. Do you need to do that?

Yes, it's going to be used both ways, to JSON and back to NamedTuple.

I realized that support for Python dataclasses is increasing quickly. In my project I can replace all the NamedTuples by dataclasses, then tools like pydantic or marshmallow-dataclass should to the job.

from typedload.

ltworf avatar ltworf commented on September 1, 2024

Well this project supports dataclasses as well.

from typedload.

ltworf avatar ltworf commented on September 1, 2024

There… now that I implemented it I might as well add support :D

import datetime
import dataclasses

import typedload
import typedload.dataloader
import typedload.datadumper


dumper = typedload.datadumper.Dumper()
dumper.handlers.append(
    (
        lambda x: isinstance(x, datetime.date),
        lambda self, value: (value.year, value.month, value.day),
    )
)

loader.handlers.append(
    (
        lambda x: x == datetime.date,
        lambda self, value, type_: datetime.date(*value),
    )
)


@dataclasses.dataclass
class Pino:
    pino: datetime.date

t = Pino(datetime.date(2019, 1, 1))
dump = dumper.dump(t)
assert loader.load(dump, Pino) == t

from typedload.

thomascellerier avatar thomascellerier commented on September 1, 2024

Hurray :)

from typedload.

ltworf avatar ltworf commented on September 1, 2024

Maybe you want to review it then? :P

from typedload.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.