viralogic / py-enumerable

A Python module used for interacting with collections of objects using LINQ syntax
MIT License
187 stars 24 forks source link

Library documentation #8

Closed viralogic closed 5 years ago

viralogic commented 7 years ago

Create a read the docs for documentation for developers using library with code samples as per #6

viralogic commented 6 years ago

In progress

houyingkun commented 6 years ago

May i know how to connect a database by this lib?I‘m just a beginner in coding,also in english :)

viralogic commented 6 years ago

Hi @houyingkun ,

There is no way to directly connect py-linq to a database. py-linq is only used for querying collections of objects that exist in memory (ie. have already been loaded into memory from a database). I am currently working on a py-queryable library that would have the LINQ API and would query databases, however it is still not complete.

To connect to a database and query, you would have to use an Object-Relational Mapping (ORM) such as sqlalchemy or TinyORM or the appropriate database Python client for the database you are wanting to connect to. For example, for a small SQLite database, you could use just execute your queries directly to SQLite using sqlite3 api that already comes with Python.

If you decide you still would like to be able to py-linq after querying your library, most query results (whether from an ORM or from a database client library) are returned as some sort of Iterable object. For example, SQLite returns a cursor after executing a SQL query. You can wrap this cursor object in an Enumerable and then use LINQ to query the result returned from the database. Please note however, that this means that the database query will be executed FIRST and then the py-linq will be executed. So depending on what you are wanting to do, this may not be the most performant approach. For example, you will want to filter in your database query rather than using where in py-linq to do the filtering for you.

If you have any more questions, please let me know. And thanks for your interest in py-linq.

houyingkun commented 6 years ago

Hi @viralogic ! Thanks for you replying my email! I would like to know what advantages of using LINQ to query data than using Sqlalchemy directly.

Then, i am finding a tech to solve the following problems: 1)Avoid database modeling: I belive that the best usage of ORM is to deal with the stable business, when i face with some lightweight task, it's very inconvenient that i have to make a model file before doing everything. 2)Avoid write everything in memory: As i know, SScursor from pymysql is a good way to deal with that(it use a iterator to get the data step by step), but i'm not sure whether sqlalchemy can deal with that. 3)using python to query db directly instead of using sql statement: Well...It looks like I'm lazy and I don't love to learn...

Could linq solve these problems?

I'll be very happy if you can anwer my question. My english seems rude because i don't know how to express in a polite way, if you feel unhappy, I apologize for this.

viralogic commented 6 years ago

@houyingkun In my own case of using SqlAlchemy to query a database, I found py-linq useful for doing data analysis on the dataset I obtained using SqlAlchemy. I could group and aggregate from this data set without having to directly hit the database again. Similar to how data people are using pandas and jupyter notebook now, but in my application code. I hope this makes sense.

In my own opinion, I believe ORMs such as sqlalchemy make an application developer's life easier. I have found that having SQL queries in your application code is OK, but to avoid duplication you end up creating classes to create, read, update, and delete for each table and you end up having to write a lot of boilerplate that is just taken care of for you by an ORM. Plus, ORM's are usually database agnostic which means you theoretically could swap out, for example, MySQL for Postgres and everything would still work. Writing your own SQL in an application would unnecessarily couple your application to the specific database vendor you decided to develop against.

In my own opinion, ORM is the way to go. Unfortunately, you will always need to create model classes as part of the necessary, database agnostic abstraction. I agree it is inconvenient to hand type all your model classes, but unfortunately this is the trade off for less tight coupling. Perhaps there is a tool that can autocreate all your database model classes for you?

As for your questions, I am sorry, but I am not fully understanding what you are asking. Could you maybe provide me with some examples of what you are trying to do?

Thanks,

Bruce

viralogic commented 5 years ago

Documentation has been created at https://viralogic.github.io/py-enumerable/