225
Points
Questions
43
Answers
49
-
Asked on July 17, 2020 in XML.
I did recreate your table from what you wrote and had success with this query:
SELECT xpath('/properties/entry[@key=''notes'']/text()', CAST(convert_from(attributes, 'UTF-8') AS XML)) AS result FROM ticketlines where ticket =1;
Though I did not reproduce your error message with your query. Please check what the real content of the ticket 7abc41d9-0d7f-4187-bb81-e4139d7728bf in your DB-Table is.
- 393 views
- 2 answers
- 0 votes
-
Asked on July 16, 2020 in XML.
Unfortunately there isn’t a built-in way that I can see in VS2019, or even an easy set of keystrokes, but if you add the Visual Commander Extension, you can add a Command to help you. Unfortunately Visual Commander is somewhat buggy, and won’t record the macro for you, but you can add it:
using EnvDTE; using EnvDTE80; public class M : VisualCommanderExt.ICommand { public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) { DTE.ExecuteCommand("Edit.ExpandSelection"); DTE.ExecuteCommand("Edit.ExpandSelection"); DTE.ExecuteCommand("Edit.ExpandSelection"); DTE.ExecuteCommand("Edit.ExpandSelection"); DTE.ExecuteCommand("Edit.ExpandSelection"); DTE.ExecuteCommand("Edit.ExpandSelection"); DTE.ExecuteCommand("Edit.ExpandSelection"); DTE.ExecuteCommand("Edit.ExpandSelection"); DTE.ExecuteCommand("Edit.ExpandSelection"); DTE.ExecuteCommand("Edit.ExpandSelection"); DTE.ExecuteCommand("Edit.SwapAnchor"); DTE.ExecuteCommand("Edit.LineStartExtend"); DTE.ExecuteCommand("Edit.LineStartExtend"); DTE.ExecuteCommand("Edit.Copy"); DTE.ExecuteCommand("Edit.NextMethod"); DTE.ExecuteCommand("Edit.NextMethod"); DTE.ExecuteCommand("Edit.LineStart"); } }
Put the cursor anywhere in a method comment block, and run the macro. It will copy the comment block to the clipboard and move the cursor to the beginning of the following method. Then you can either paste, or click to put the cursor at the beginning of another method and paste.
- 412 views
- 1 answers
- 0 votes
-
Asked on July 16, 2020 in .NET.
I didn’t think there was anything wrong with Mong Zhu’s answer, but it’s now deleted
If you know absolutely for certain that all the buttons on the list have a tag and it’s always a ButtonTag, you can just straight cast and retrieve the hour
.Where(x => ((ButtonTag)x.Tag).IsHour)
Only you will know whether it is always the case that everything in the list is a Button with a ButtonTag in its Tag. If it is not always the case, look at something like Pavel’s answer, especially if (in the future) someone might add some code that suddenly means the tags aren’t always ButtonTags
To understand why your original attempt went wrong you have to appreciate that Select typically creates new kinds of objects from your list of original objects and, unless there is a way to retrieve the original object from the new you’ve lost the original object.
Your select pulled out just the ButtonTag, so unless this class type has a reference to the button that once owned it (for example a ParentButton property that refers to the button the tag is assigned to) you can’t retrieve the original object any more
If you’re in a situation like that, you can do as all the solutions suggest here, that the casting is done in the where (because Where just returns original objects that satisfy a condition, I.e it returns Buttons) or you can consider making it so the Select returns the original object as well as any new things you’re creating:
.Select(x => new { OriginalButton = x, ButtonTag = (ButtonTag)x.Tag })
This prepares a new anonymous type with our derived/calculated/new info as well as the original info that drove it
You probably wouldn’t do this in this case, because it’s so trivial as to not need it, but you might one day be faced with needing to return all the original info about an object plus some new stuff you calculated and a simple way to do that is to include the original as a property along with the new stuff via something like this
- 422 views
- 3 answers
- 0 votes
-
Asked on July 16, 2020 in .NET.
You could use reflection and loop through all the object properties, then get their values and save them to the log. The formatting is really trivial (you could use \t to indent an objects properties and its values):
MyObject Property1 = value Property2 = value2 OtherObject OtherProperty = value ...
- 737 views
- 13 answers
- 0 votes
-
Asked on July 16, 2020 in .NET.
Legacy Code’s comment answered my question:
Because you have fields not properties. Obviously… And don’t forget the naming nomenclature. Properties are PascalCase not camelCase.
all I needed to do was set the accessors
- 407 views
- 1 answers
- 0 votes
-
Asked on July 16, 2020 in .NET.
I would like to show some code according to my understanding, hopefully it can help somebody.
As aepot, dymanoid and Hans Passant (thanks to them) said, using the default
SynchronizationContext
will do nothing more thanPost
ing the rest of the code afterawait
ing to theSynchronizationContext
.I created a very very basic and NOT optimal
SynchronizationContext
to demonstrate how the basic implementation should look like. My implementation will create a newThread
and run someTask
s in a specific context inside the same newly createdThread
.Better implementation (but much complex) may be found here in Stephen Cleary’s GitHub repository.
My implementation looks basically like following (from my GitHub repository, the code in the repository may look different in the future):
/// <summary> /// This <see cref="SynchronizationContext"/> will call all posted callbacks in a single new thread. /// </summary> public class SingleNewThreadSynchronizationContext : SynchronizationContext { readonly Thread _workerThread; readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> _actionStatePairs = new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>(); /// <summary> /// Returns the Id of the worker <see cref="Thread"/> created by this <see cref="SynchronizationContext"/>. /// </summary> public int ManagedThreadId => _workerThread.ManagedThreadId; public SingleNewThreadSynchronizationContext() { // Creates a new thread to run the posted calls. _workerThread = new Thread(() => { try { while (true) { var actionStatePair = _actionStatePairs.Take(); SetSynchronizationContext(this); actionStatePair.Key?.Invoke(actionStatePair.Value); } } catch (ThreadAbortException) { Console.WriteLine($"The thread {_workerThread.ManagedThreadId} of {nameof(SingleNewThreadSynchronizationContext)} was aborted."); } }); _workerThread.IsBackground = true; _workerThread.Start(); } public override void Post(SendOrPostCallback d, object state) { // Queues the posted callbacks to be called in this SynchronizationContext. _actionStatePairs.Add(new KeyValuePair<SendOrPostCallback, object>(d, state)); } public override void Send(SendOrPostCallback d, object state) { throw new NotSupportedException(); } public override void OperationCompleted() { _actionStatePairs.Add(new KeyValuePair<SendOrPostCallback, object>(new SendOrPostCallback(_ => _workerThread.Abort()), null)); _actionStatePairs.CompleteAdding(); } }
and here is a Demo to use it:
static void SingleNewThreadSynchronizationContextDemo() { var synchronizationContext = new SingleNewThreadSynchronizationContext(); // Creates some tasks to test that the whole calls in the tasks (before and after awaiting) will be called in the same thread. for (int i = 0; i < 20; i++) Task.Run(async () => { SynchronizationContext.SetSynchronizationContext(synchronizationContext); // Before yielding, the task will be started in some thread-pool thread. var threadIdBeforeYield = Thread.CurrentThread.ManagedThreadId; // We yield to post the rest of the task after await to the SynchronizationContext. // Other possiblity here is maybe to start the whole Task using a different TaskScheduler. await Task.Yield(); var threadIdBeforeAwait1 = Thread.CurrentThread.ManagedThreadId; await Task.Delay(100); var threadIdBeforeAwait2 = Thread.CurrentThread.ManagedThreadId; await Task.Delay(100); Console.WriteLine($"SynchronizationContext: thread Id '{synchronizationContext.ManagedThreadId}' | type '{SynchronizationContext.Current?.GetType()}.'"); Console.WriteLine($"Thread Ids: Before yield '{threadIdBeforeYield}' | Before await1 '{threadIdBeforeAwait1}' | Before await2 '{threadIdBeforeAwait2}' | After last await '{Thread.CurrentThread.ManagedThreadId}'.{Environment.NewLine}"); }); } static void Main(string[] args) { Console.WriteLine($"Entry thread {Thread.CurrentThread.ManagedThreadId}"); SingleNewThreadSynchronizationContextDemo(); Console.WriteLine($"Exit thread {Thread.CurrentThread.ManagedThreadId}"); Console.ReadLine(); }
Output:
Entry thread 1 Exit thread 1 SynchronizationContext: thread Id '5' | type 'SynchronizationContexts.SingleNewThreadSynchronizationContext.' Thread Ids: Before yield '11' | Before await1 '5' | Before await2 '5' | After last await '5'. SynchronizationContext: thread Id '5' | type 'SynchronizationContexts.SingleNewThreadSynchronizationContext.' Thread Ids: Before yield '4' | Before await1 '5' | Before await2 '5' | After last await '5'. SynchronizationContext: thread Id '5' | type 'SynchronizationContexts.SingleNewThreadSynchronizationContext.' Thread Ids: Before yield '12' | Before await1 '5' | Before await2 '5' | After last await '5'. SynchronizationContext: thread Id '5' | type 'SynchronizationContexts.SingleNewThreadSynchronizationContext.' Thread Ids: Before yield '6' | Before await1 '5' | Before await2 '5' | After last await '5'. SynchronizationContext: thread Id '5' | type 'SynchronizationContexts.SingleNewThreadSynchronizationContext.' Thread Ids: Before yield '10' | Before await1 '5' | Before await2 '5' | After last await '5'. SynchronizationContext: thread Id '5' | type 'SynchronizationContexts.SingleNewThreadSynchronizationContext.' Thread Ids: Before yield '7' | Before await1 '5' | Before await2 '5' | After last await '5'.
- 0 views
- 2 answers
- 0 votes
-
Asked on July 16, 2020 in Mysql.
You can use the IN as
select * from events where (id,grab_time) in (SELECT id, MAX(`grab_time`) AS `grab_time` FROM events WHERE ended = false GROUP BY id)
You can also use the
MAX
windows function as follows:select * from ( select t.*, max(grab_time) over (partition by id) as mx from your_table where ended = false ) where grab_time = mx
- 359 views
- 3 answers
- 0 votes
-
Asked on July 16, 2020 in Python.
Try using below import:
from Project.RestApi.query_generator import *
- 410 views
- 2 answers
- 0 votes
-
Asked on July 16, 2020 in Python.
imgs = read_given_images("/content/images2",df.names.values) points = df['labels'].values points = np.array(points)
The error was in a zip, i imported zipfile. which i shouldn’t have. python was this function built-in. Thanks.
- 357 views
- 1 answers
- 0 votes
-
Asked on July 16, 2020 in Python.
I just had this same problem and I just solved it this week. My problem was not getting the includes to work properly to get the online code that I found working properly.
I am going to assume you mean change the background color not change the font color. If I am wrong clarify your request.
My solution is tied to a particular library. openpyxl
#### This import section is where my mistake was at #### This works for me import openpyxl ### Excel files from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font from openpyxl.styles import Fill, Color from openpyxl.styles import Style from openpyxl.styles.colors import RED from openpyxl.styles.colors import GREEN str_xls_PathFileCurrent = str_xls_FileName ### Opens Excel Document var_xls_FileOpen = openpyxl.load_workbook(str_xls_PathFileCurrent) ### Opens up the Excel worksheet var_xls_TabName = var_xls_FileOpen.worksheets[0] ### Put the spreadsheet tab names into an array ary_xls_SheetNames = var_xls_FileOpen.get_sheet_names() ### Open the sheet in the file you working on var_xls_TabSheet = var_xls_FileOpen.get_sheet_by_name(ary_xls_SheetNames[0]) xls_cell = var_xls_TabSheet['d10'] #### Changes the cell background color xls_cell.style = Style(fill=PatternFill(patternType='solid' , fgColor=Color('C4C4C4'))) ### Changes background color #### Changes the fonts (does not use style) xls_cell.font = xls_cell.font.copy(color = 'FFFF0000') ### Works (Changes to red font text) xls_cell.font = xls_cell.font.copy(bold = True) ### Works (Changes to bold font) xls_cell.font = xls_cell.font.copy(italic= True) ### Works (Changes to Italic Text) xls_cell.font = xls_cell.font.copy(size = 34) ### Works (Changes Size)
- 403 views
- 2 answers
- 0 votes