I created a ActiveRecord model that does not have a corresponding table in the database. I want it to manage the application 'scoped' queries:
class Meter <ActiveRecord::Base
def self.daily_tests
self.connection.execute(sanitize_sql(["SELECT b.*, m.*, i.* FROM bgtests AS b JOIN meals AS m ON b.user_id = m.user_id JOIN injections AS i ON i.user_id = m.user_id WHERE b.user_id = 2 AND m.user_id = 2 AND i.user_id = 2"]))
end
end
The query executes however the result set brings back this:
{"id"=>1, "value"=>712, "category"=>"basal", "time_of_day"=>"Before dinner", "comments"=>"Est et aut. Est maxime sunt. Dolor doloribus distinctio sed reprehenderit culpa. Autem ipsam atque modi dolor ut non. Aut dicta voluptate occaecati.", "user_id"=>2, "created_at"=>"2015-06-21 18:58:55.806367", "updated_at"=>"2015-06-21 18:58:55.806367", "name"=>"snack", "carbohydrates"=>142, "description"=>"Facere reiciendis non officia velit consequatur voluptas eum. Veritatis quia cumque. Dolor non eaque quod. Dignissimos quae aut eveniet sunt ea amet. Iste et aut unde consequatur quia commodi.", "num_of_units_taken"=>1.0, 0=>10, 1=>712, 2=>"smbg", 3=>"Before dinner", 4=>"Est et aut. Est maxime sunt. Dolor doloribus distinctio sed reprehenderit culpa. Autem ipsam atque modi dolor ut non. Aut dicta voluptate occaecati.", 5=>2, 6=>"2015-06-21 18:58:55.595625", 7=>"2015-06-21 18:58:55.595625", 8=>12, 9=>"snack", 10=>142, 11=>"Facere reiciendis non officia velit consequatur voluptas eum. Veritatis quia cumque. Dolor non eaque quod. Dignissimos quae aut eveniet sunt ea amet. Iste et aut unde consequatur quia commodi.", 12=>2, 13=>"2015-06-21 18:58:56.115615", 14=>"2015-06-21 18:58:56.115615", 15=>1, 16=>1.0, 17=>"basal", 18=>2, 19=>"2015-06-21 18:58:55.806367", 20=>"2015-06-21 18:58:55.806367"},
The initial key-values are return as column-value however it then switches and adds subsequent records as 5=>2 for example.
My question is how can I query these multiple models on the foreign_key 'user_id" and have the result set bring back all the results in the typical object notation specific to the model?
Most of the methods defined by the ActiveRecord query interface accept SQL string arguments in addition to other forms specified in the docs. You can also arbitrarily chain these methods, and AR will combine to create a proper SQL. When in doubt, use to_sql to view the SQL generated. Assuming bgtests corresponds to a model called Bgtest, you could for example do the following:
Bgtest.select("b.*, m.*, i.*").from("bgtests b")
.joins("JOIN meals AS m ON b.user_id = m.user_id")
.joins("JOIN injections AS i ON i.user_id = m.user_id")
.where("b.user_id = 2 AND m.user_id = 2 AND i.user_id = 2")
Method chaining works this way because each method returns an ActiveRecord::Relation object that accepts the same ActiveRecord::QueryMethods methods as ActiveRecord::Base (which your models subclass). Ordering of the method calls is therefore arbitrary. The SQL is not actually executed until you attempt to extract a value from the relation, whether due to the console calling to_s or because you call each in a view.
Note that the above can be greatly simplified (and much more "Rails-y") by defining the correct associations on the models. In particular, the classes Bgtest, Meal, and Injection would all apparently call belongs_to :user, and User would call has_many :bgtests, etc. You can then use has_many with :through option in other classes, e.g. Bgtest could call has_many :meals, through: user, etc. AR would then know to INNER JOIN based on referencing the association names alone:
Bgtest.joins(:meals, :injections).where(user_id: 2)
or LEFT OUTER JOIN:
Bgtest.includes(:meals, :injections).where(user_id: 2)
Related
I have a big dataframe (over 20 000 rows) and I want to apply Spacy (v3) to it. I need all the components of the spacy pipeline. I already tried with apply (row-wise) but it takes forever:
df = pd.read_csv(f, sep='\t', encoding='utf-8')
df['Text_spacy'] = df['Text_initial'].apply(lambda x: nlp(x))
display(df)
The column Text initial contains something like :
Text_initial
Lorem ipsum dolor sit amet, consectetur adipiscing elit.Ut enim ad minim veniam.Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Sunt in culpa qui officia deserunt mollit anim id est laborum.
Pharetra magna ac placerat vestibulum lectus. Nec feugiat nisl pretium fusce id velit ut. Amet justo donec enim diam vulputate ut pharetra.Nibh venenatis cras sed felis eget velit aliquet sagittis id.
Notice that in every row, the phrases are already separated by a \n. The segmentation will be made based on the \n. Thus, my question is:
Is there a faster way to apply spacy to each row of the df in a loop ? Iterrows() is even slower. Would a batch (e.g. the first 100 rows, then the next 100 ... till the end) be faster ?
nlp.pipe() expects an iterable of strings, so try this:
df = pd.read_csv(f, sep='\t', encoding='utf-8')
df['Text_spacy'] = [d for d in nlp.pipe(df['Text_initial'])]
Although the question is what do you want to get from SpaCy (Tokenization, Lemmatization, POS etc.). Because applying it like this will just put the Doc objects into your DataFrame (which are then represented as the tokens string tuples for printing).
Function's arguments inside switch are wrong formatted. Longest line in this case ends in column 193 , although set 'code_width = 120' I hope it's my fault in config formatter.
int mian(void)
{
switch (event) {
case foo:
Bar(foo,
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras nunc diam, eleifend nec posuere at, viverra vitae velit %d, %d, %d", parameterA, parameterB, parameterC, parameterD,
parameterE);
break;
default:
break;
}
}
code_width = 120
# nl_func_call_args_multi_line = true # not used
# nl_func_call_args = ignore # not used
Maybe someone had same issue?
I would like to add horizontal border of cell in my LaTeX table (or completely redefine the table to get the same result). My current definition is:
\begin{table}[h!]
\resizebox{\textwidth}{!}{%
\begin{tabular}{
>{\columncolor[HTML]{EFEFEF}}l l}
\hline
\multicolumn{2}{l}{\cellcolor[HTML]{EFEFEF}\textbf{1^{st}} \textbf{Category Title}} \\
\hline
Description &
\begin{tabular}{p{20cm}}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
\end{tabular} \\
Effect &
\begin{tabular}{p{20cm}}
Proin in tellus sit amet nibh dignissim sagittis. Integer in sapien. Class aptent
taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.
\end{tabular} \\
\hline
\end{tabular}%
}
\caption{Category table}
\label{tab:categ_list}
\end{table}
which looks like this:
And I would like to add horizontal line to split cells with text:
I've found a solution with \arrayrulecolor[HTML]{CFCFCF}\hline which looks quite good but \hline separates whole row, not only a single cell. I've also tried \cline but this was not working (no cell border appeared and moreover whole row disappeared).
You can add one
\cline{2-2}
in the code line before before Effect.
I have simplified your code for two reasons:
the header was originally missing and I didn't know what package(s) to use to run your syntax for the colors (so, sorry but colors are missing below);
I do firmly believe that nesting more than one tabular environment is not necessary, you can make it with more minimal and tidy code.
My sketch:
\documentclass{article}
\usepackage{multicol}
\def\arraystretch{1.5}
\begin{document}
\begin{table}%[h!]
\begin{tabular}{lp{10cm}}
\hline
\multicolumn{2}{l}{$\mathbf{1^{st}}$\textbf{ Category Title}}\\
\hline
Description & Lorem ipsum dolor sit amet, consectetuer adipiscing elit\\
\cline{2-2}
Effect & Proin in tellus sit amet nibh dignissim sagittis. Integer in sapien.
Class aptent taciti sociosqu ad litora torquent per conubia nostra,
per inceptos hymenaeos.\\
\hline
\end{tabular}
\caption{Category table}
\label{tab:categ_list}
\end{table}
\end{document}
This outputs:
I hope this helps you, otherwise please comment!
Finally, as in the comment to your question by #samcarter, you may check the package booktabs for this and future tables in this style with no vertical border lines.
Hi I am using the following packages, I tried different methods to change the font size but in vain (extsize, luximono, anyfontsize). These are the packages I use (quite a lot actually), I suspect that one package is interfering in the process. If you can help, thank you.
\documentclass{report}
\usepackage[scaled=0.87]{luximono}
%\usepackage{extsizes}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{epsfig}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{mathdesign}
\usepackage{lmodern,textcomp}
\usepackage{eurosym}
\usepackage{endnotes}
\usepackage{amssymb}
\usepackage{ifthen}
\usepackage{calc}
\usepackage{cancel}
\usepackage{capt-of}
\usepackage{cite}
\usepackage{here}
\usepackage{fullpage}
\usepackage{titlesec}
%\usepackage{subfigure}
\titleformat{\chapter}[hang]{\bf\huge}{\thechapter}{2pc}{}
\usepackage{lastpage}
\usepackage{array}
\usepackage{hyperref}
\usepackage{amsmath,amsfonts,amssymb,amsthm,epsfig,epstopdf,titling,url,array,xpatch}
\usepackage{listings}
\usepackage{color}
\usepackage{float}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
There is an easy global way to make it: \fontsize{X}{Y}\selectfont sets the default font size to Xpt and the baselineskip to Ypt (Y should be about 1.2 times X).
Here is a minimal working example for your case, where I cut all the packages not dealing with our purpose but I left your title formatting (this latter is not affected by \fontsize).
\documentclass{report}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{titlesec}
\titleformat{\chapter}[hang]{\bf\huge}{\thechapter}{2pc}{}
\begin{document}
\fontsize{7}{9}\selectfont
\chapter{Example}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vivamus at lorem vel felis consectetur hendrerit ut nec neque.
\chapter{Example}
Sed eget nunc leo. Nulla efficitur neque diam, aliquet interdum dolor gravida nec.
\end{document}
In latex the default size is passed as an option to the documentclass, so that other document elements get appropriate sizes too. The standard documentclasses only support 10pt, 11pt and 12pt. The extsizes package changes this. Here is an example: use the extreport class instead of report and pass the size as an option as follows.
\documentclass[7pt]{extreport}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\begin{document}
\section{A section}
\label{sec:section}
Some text in my report.
\end{document}
I need to replace the tag {URL}:
DECLARE #PageUrl varchar(200)
DECLARE #Body varchar(MAX)
SET #PageUrl = 'http://www.website.com/site1/site2/pageName.asxp?rid=1232'
SET #Body = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed luctus,
{URL} enim nec posuere volutpat, neque dui volutpat turpis. '
SET #Body = REPLACE(#Body,'{Url}', CONVERT(varchar,#PageUrl))
PRINT #Body
My expected result is:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed luctus,
http://www.website.com/site1/site2/pageName.asxp?rid=1232 enim nec posuere volutpat, neque dui volutpat turpis.
And the print result is:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed luctus,
http://www.website.com/site1/s enim nec posuere volutpat, neque dui volutpat turpis.
As you can see the replace function cuts the url string at its 31...
What I'm doing wrong?
The problem is not the replace method , it is the convert method..
You need to either specify the length of the converted type
SET #Body = REPLACE(#Body,'{Url}', CONVERT(varchar(200),#PageUrl))
or since it is already defined as a varchar just use the variable..
SET #Body = REPLACE(#Body,'{Url}', #PageUrl)
If you have a look at the char/vachrar page
When n is not specified in a data
definition or variable declaration
statement, the default length is 1.
When n is not specified when using the
CAST and CONVERT functions, the
default length is 30.
You don't specify a varchar length:
CONVERT(varchar,#PageUrl)
guess what the default is? 30, just like you say in the question (the 31st char to the end is missing)
use it like this:
CONVERT(varchar(200),#PageUrl)
to get all the characters
It's getting cut off at this line:
SET #Body = REPLACE(#Body,'{Url}', CONVERT(varchar,#PageUrl))
Use this:
SET #Body = REPLACE(#Body,'{Url}', CONVERT(varchar(200),#PageUrl))