Git Product home page Git Product logo

genshin-py's Introduction

genshin-py

A parser for all data from Dimbreath/GenshinData. This is written as a module, which provides a RepoData class, can be initialized from a path to DimBreath's repository folder, or by separately specifying Excel and TextMap folders.

As the nature of the fast-evolving of the repository data, genshin-py will not be provided in pypi, or other package distribution sites, please remember to git pull from the repository as new version of GenshinData released to keep up the changes, furthermore, please try to fetch the newest commit first when you come across bugs and errors, before submitting issues.

Using Python 3.9.1, still much work-in-progress. The parser is developed under Microsoft Pylance Language server, for difference in development experience, especially when comes with type-hinting and things alike, it should not be the problem in the code.

1. Object Abstraction

genshin-py comes with multiple objects to make a abstract and detailed summary over the complex json structure. Mainly, it comes with three classes : JsonAdapter, ConfigAdapter and RepoData.

1.1 RepoData

RepoData is simple : a collection of all json configs parsed in the currenct structure. There's no actual need to adjust most of the content in this object, as the config files should be stable, or you can write methods to extend its functions for more analytical usage.

1.2 ConfigAdapter

ConfigAdapter represents an abstraction for any single config file in the repository, with a assumption of the data starts as [object1, object2...] as basic structure. For extending the parser to parse config files that's not yet implemented, please write a subclass that inherits the ConfigAdapter[T] base class, or the MappedAdapter[T] base class if a id annotation is in the class to index the objects.

When you mark a class which inherits from ConfigAdapter[T], this marks the class to accept a List[T] in __init__ to construct the object, where T must be a instance of JsonAdapter:

class Adapter(JsonAdapter):
    id : Adapter('Id', int)

class AdapterConfig(MappedAdapter[Adapter]):
    pass

# Instantiate
inst = AdapterConfig([{'Id':1},{'Id':2}])
print(inst.entries[0].id) # 1
print(inst[2].id) # 2
print(list(inst.mappings.keys())) # [1,2]

Please register corresponding json parsers in the RepoData class to keep everything simple.

1.3 JsonAdapter

JsonAdapter represents an entry in the List[T] parsed by json decoder. Which is designed to parse a json object into correct python object. Marking a class inherits from JsonAdapter makes the class to accept a Dict of json object to construct the object.

To ensure well-defined type hints and type-checking in both development and runtime, a annotation-based auto adapter was implemented in JsonAdapter to generate most of the logic automatically.

class Adapter(JsonAdapter):
    id : Adapter('Id', int)
    is_even : Adapter('Number', bool, transformer=lambda x:x%2==0)
    fallback : Adapter('Fallible', int, fallback=lambda x: x["Id"] - 1)

# Instantiate 
inst = Adapter({"Id":1, "Number":3})
print(inst.id) # 1
print(inst.is_even) # False
print(inst.fallback) # 0

For advanced usages, please refer to docs and examples already written in configs.

genshin-py's People

Contributors

ariette avatar prunoideae avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

milkve

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.